Jump to content

place holder text in input field


Mr.Canuck

Recommended Posts

Hi everyone. In the script below, I would like to have the input box for "Website" to have http:// already in that input field. So, people will see "Website:" and then in the input field it will have "http://"  I'm a PHP novice, and I'm not sure how to do it. Thanks.

 

<?php

$contact_form_fields = array(
  array('name'    => 'Website:',
        'type'    => 'input',
        'require' => 1),
  array('name'    => 'Name:',
        'type'    => 'name',
        'require' => 1),
  array('name'    => 'E-mail:',
        'type'    => 'email',
        'require' => 1),
  array('name'    => 'Phone:',
        'type'    => 'input',
        'require' => 0),
  array('name'    => 'Service:',
        'type'    => 'select',
        'require' => 1,
        'default' => 'Select Desired Service',
        'items'   => array('Keyword Research',
                           'Search Engine Optimization',
					   'Link Building Services',
					   'SEO Web Design',
					   'SEO Copywriting',
					   'SEO Consulting',
					   'Conversion Optimization',
					   'Social Media and PR',
					   'Landing Page Optimization',
					   'Other')),
  array('name'    => 'Budget:',
        'type'    => 'select',
        'require' => 1,
        'default' => 'Budget / Monthly',
        'items'   => array('$500-$1500',
                           '$1500-$2500',
					   '$2500-$5000',
					   '$5000-$10000',
					   '$10K +')),
  array('name'    => 'Comments:',
        'type'    => 'textarea',
        'require' => 1),
  array('name'    => 'Turing number:',
        'type'    => 'turing',
        'require' => 1,
        'url'     => 'contact-form/image.php',
        'prompt'  => 'Please enter the number displayed above'),
);

$contact_form_graph           = false;
$contact_form_xhtml           = true;

$contact_form_email           = "someone@somewhere.com";
$contact_form_encoding        = "utf-8";
$contact_form_default_subject = "Sent from free SEO quote form";
$contact_form_message_prefix  = "Sent from free SEO quote form\r\n==============================\r\n\r\n";

include_once "contact-form2/contact-form/contact-form.php";

?>

Link to comment
Share on other sites

Thanks for the reply. Here is the contact-form.php:

<?php

?>


<div class="contact_form">


<!-- ***** Config ********************************************************** -->

<?php

$contact_form_msg_clear    = 'Clear';
$contact_form_msg_submit   = '';
$contact_form_msg_submit   = $contact_form_graph ? '' : $contact_form_msg_submit;

$contact_form_msg_sent     = 'Message sent';
$contact_form_msg_not_sent = 'Message not sent';
$contact_form_msg_invalid  = 'Please, correct the fields marked with red';

?>


<!-- ***** PHP ************************************************************* -->

<?php

// ***** contact_form_mail *****

function contact_form_mail($to, $domain, $message, $headers = '', $charset = 'utf-8', $files = array())
{
  if (!count($files))
  {
    $ext_headers  = $headers;
    $ext_headers .= "Content-Type: text/plain; charset=\"$charset\"\r\n";
    $ext_message  = $message;
  }
  else
  {
    $boundary = 'a6cd792e';
    while (true)
    {
      if (strpos($domain, $boundary) !== false ||
          strpos($message, $boundary) !== false) { $boundary .= dechex(rand(0, 15)) . dechex(rand(0, 15)); continue; }
      foreach ($files as $fi_name => $fi_data)
      if (strpos($fi_name, $boundary) !== false ||
          strpos($fi_data, $boundary) !== false) { $boundary .= dechex(rand(0, 15)) . dechex(rand(0, 15)); continue; }
      break;
    }

      $ext_headers  = $headers;
      $ext_headers .= "MIME-Version: 1.0\r\n";
      $ext_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

      $ext_message  = "This is a multi-part message in MIME format.";
      $ext_message .= "\r\n--$boundary\r\n";

      $ext_message .= "Content-Type: text/plain; charset=\"$charset\"\r\n\r\n";
      $ext_message .= $message;
      $ext_message .= "\r\n--$boundary\r\n";

    foreach ($files as $i => $x)
    {
      $ext_message .= "Content-Type: {$x['type']}; name=\"{$x['name']}\"\r\n";
      $ext_message .= "Content-Disposition: attachment\r\n";
      $ext_message .= "Content-Transfer-Encoding: base64\r\n\r\n";
      $ext_message .= chunk_split(base64_encode($x['data']));
      $ext_message .= "\r\n--$boundary\r\n";
    }
  }

  $error_reportings = error_reporting(E_ERROR | E_PARSE);
  $res = mail($to, $domain, $ext_message, $ext_headers);
  $error_reportings = error_reporting($error_reportings);

  return $res;
}

// ***** contact_form_post *****

function contact_form_post($name)
{
  global $contact_form_encoding;
  if (isset($_POST[$name])) return htmlentities($_POST[$name], ENT_COMPAT, $contact_form_encoding);
  if (isset($_GET [$name])) return htmlentities($_GET [$name], ENT_COMPAT, $contact_form_encoding);
  return '';
}

// ***** Send Mail *****

if (count($_POST))
{
  if (get_magic_quotes_gpc() && !function_exists('strip_slashes_deep'))
  {
    function strip_slashes_deep($value)
    {
      if (is_array($value)) return array_map('strip_slashes_deep', $value);
      return stripslashes($value);
    }

    $_GET    = strip_slashes_deep($_GET);
    $_POST   = strip_slashes_deep($_POST);
    $_COOKIE = strip_slashes_deep($_COOKIE);
  }

  $patern_aux1 = "(\\w+(-\\w+)*)";
  $patern_aux2 = "($patern_aux1\\.)*$patern_aux1@($patern_aux1\\.)+$patern_aux1";

  $ename = '';
  $email = '';
  $esubj = $contact_form_default_subject;
  $ehead = $contact_form_message_prefix;
  $ebody = '';
  $valid = true;
  foreach ($contact_form_fields as $i => $x)
  {
    $_POST[$i] = isset($_POST[$i]) ? $_POST[$i] : '';

    if ($x['type'] === 'upload')
    {
      if (isset($_POST["$i-clear"]) && $_POST["$i-clear"])
          unset($_SESSION['contact-form-upload'][$i]);
      if (isset($_FILES[$i])             &&
                $_FILES[$i][    'type']  &&
                $_FILES[$i][    'name']  &&
                $_FILES[$i]['tmp_name']  &&
    file_exists($_FILES[$i]['tmp_name']) &&
       filesize($_FILES[$i]['tmp_name']) <= $x['maxsize'])
                $_SESSION['contact-form-upload'][$i] =
                    array('type' =>                   $_FILES[$i][    'type'],
                          'name' =>                   $_FILES[$i][    'name'],
                          'data' => file_get_contents($_FILES[$i]['tmp_name']));
    }

    if ($x['type'] === 'checkbox'   && trim($_POST[$i]) ||
        $x['type'] === 'department' && trim($_POST[$i]) ||
        $x['type'] === 'input'      && trim($_POST[$i]) ||
        $x['type'] === 'name'       && trim($_POST[$i]) ||
        $x['type'] === 'select'     && trim($_POST[$i]) ||
        $x['type'] === 'domain'    && trim($_POST[$i]) ||
	$x['type'] === 'budget'    && trim($_POST[$i]) ||
        $x['type'] === 'textarea'   && trim($_POST[$i]) ||
        $x['type'] === 'email'      && preg_match("/^$patern_aux2$/sDX", $_POST[$i]) ||
        $x['type'] === 'turing'     && isset($_SESSION['contact-form-number']) && $_POST[$i] === $_SESSION['contact-form-number'] ||
        $x['type'] === 'upload'     && isset($_SESSION['contact-form-upload'][$i]))
    {
      if ( $x['type'] === 'textarea')
           $ebody .=             "\r\n" . $_POST[$i] . "\r\n";

      if ( $x['type'] !== 'textarea')
      if (!$x['name'] && isset($x['prompt']))
           $ehead .= $x['prompt'] . ' ' . $_POST[$i] . "\r\n";
      else $ehead .= $x['name'  ] . ' ' . $_POST[$i] . "\r\n";
    }
    elseif ($x['require'] || $_POST[$i] !== '')
    {
      $valid = false;
      if (!$x['name'] && isset($x['prompt']))
           $contact_form_fields[$i]['prompt'] = "<em>{$x['prompt']}</em>";
      else $contact_form_fields[$i]['name'  ] = "<em>{$x['name'  ]}</em>";
    }

    switch ($x['type'])
    {
      case 'department': foreach ($x['items'] as $j => $y) if ($y === $_POST[$i]) $contact_form_email = $j; break;
      case 'email':      $email = $_POST[$i]; break;
      case 'name':       $ename = $_POST[$i]; break;
      case 'domain':    $edomain = $_POST[$i]; break;
  case 'budget':    $ebudget = $_POST[$i]; break;
    }
  }

  if ($valid)
  {
    $mail_sent = contact_form_mail($contact_form_email, $esubj, $ehead . $ebody,
                                   "From: $ename <$email>\r\n", $contact_form_encoding,
                                    isset($_SESSION['contact-form-upload']) ? $_SESSION['contact-form-upload'] : array());

    if ($mail_sent)
         echo '<div class="error"><em>'               . $contact_form_msg_sent     . '</em></div>';
    else echo '<div class="error"><em class="error">' . $contact_form_msg_not_sent . '</em></div>';
    if ($mail_sent) $_POST    = array();
    if ($mail_sent) $_SESSION = array();
  }
  else   echo '<div class="error"><em>'               . $contact_form_msg_invalid  . '</em></div>';
}

$_SESSION['contact-form-number'] = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);

?>


<!-- ***** HTML ************************************************************ -->

<form style="margin-top:20px; margin-left:-20px" method="post" action="<?=$_SERVER['REQUEST_URI'];?>" enctype="multipart/form-data">
<table>
<?php

$slash = $contact_form_xhtml ? '/' : '';
foreach ($contact_form_fields as $i => $x)
{
  ?>
  <tr>
  <th><?=$x['name'];?></th>
  <td>
  <?php
  switch ($x['type'])
  {
    case 'name':
    case 'email':
    case 'input':

      ?>
      <div class="input"><input name="<?=$i;?>" type="text" value="<?=contact_form_post($i);?>" <?=$slash;?>></div>
      <?php
      break;
    case 'turing':
      ?>
      <div class="input"><input name="<?=$i;?>" type="text" value="<?=contact_form_post($i);?>" <?=$slash;?>></div>
      <img width="60" height="17" src="<?=$x['url'];?>?sname=<?=session_name();?>&rand=<?=rand();?>" alt="" <?=$slash;?>>
      <br style="clear: both;" <?=$slash;?>>
      <small><?=$x['prompt'];?></small>
      <?php
      break;
    case 'upload':
      ?>
      <input name="<?=$i;?>" type="file" value="<?=contact_form_post($i);?>" <?=$slash;?>>
      <?php
      if (isset($_SESSION['contact-form-upload'][$i]))
      {
        ?>
        <input name="<?=$i;?>-clear" type="checkbox" value="Yes" <?=$slash;?>>
        <?=$contact_form_msg_clear;?> <?=$_SESSION['contact-form-upload'][$i]['name'];?>
        <?php
      }
      break;
    case 'checkbox':
      ?>
      <input name="<?=$i;?>" type="checkbox" value="Yes" <?=contact_form_post($i) ? 'checked="checked"' : '';?> <?=$slash;?>>
      <small><?=$x['prompt'];?></small>
      <?php
      break;
    case 'textarea':
      ?>
      <div class="input"><textarea name="<?=$i;?>" cols="30" rows="6"><?=contact_form_post($i);?></textarea></div>
      <?php
      break;
    case 'select':
    case 'department':
      ?>
      <select name="<?=$i;?>">
      <option value=""><?=$x['default'];?></option>
      <?php foreach ($x['items'] as $j => $y) { ?><option value="<?=$y;?>" <?=contact_form_post($i) === $y ? 'selected="selected"' : '';?>><?=$y;?></option><?php } ?>
      </select>
      <?php
      break;
  }
  ?>
  </td>
  </tr>
  <?php
}

?>

<tr><th></th><td><input id="submit_contact" class="submit" type="submit" value="<?=$contact_form_msg_submit;?>" <?=$slash;?>></td></tr>

</table>
</form>


</div>

Link to comment
Share on other sites

in the array build, inject in place of

array('name'    => 'Website:',
        'type'    => 'input',
        'require' => 1),

array('name'    => 'Website:',
        'type'    => 'input',
        'require' => 1,
        'pre' => 'http://',
        'post' => '.com'),

now in the contact-form

replace

?>
      <div class="input"><input name="<?=$i;?>" type="text" value="<?=contact_form_post($i);?>" <?=$slash;?>></div>
      <?php

if(isset($x['pre'])||isset($x['post']))
  {
	$str = contact_form_post($i);
	if(isset($x['pre'])&&substr($str,0,strlen($x['pre']))==$x['pre'])
	{
	  $str=substr($str,strlen($x['pre']));
	}
	if(isset($x['pre'])&&substr($str,(strlen($str)-strlen($x['post'])))==$x['post'])
	{
	  $str=substr($str,0,(strlen($str)-strlen($x['post'])));
	}
	$_POST[$i]=$str;
  }
 ?>
      <div class="input"><input name="<?=$i;?>" type="text" value="<?= $x['pre']; ?><?=contact_form_post($i);?><?= $x['post']; ?>" <?=$slash;?>></div>
      <?php

run a quick test and looks ok :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.