Setting default values help

Hi All,

First post and all that, hope you can help.

Basically I have an implementation of the PHP email form from here:

http://boxmodeljunkie.com/create-a-simple-php-contact-form/

and want there to be an initial value for the form components saying Name, Email Address and Message body, rather than the Form Labels, I have tried everything, but as this seems to be the variables being delcared and contains a capture of the input I cant change it.

//assign post data to variables
$name = trim($_POST[‘name’]);
$email = trim($_POST[‘email’]);
$message = trim($_POST[‘message’]);

Any help would be greatly appreciated, oh and I am very new to PHP.

Thanks

Stephen

Hey simply add values to the fields, and with text areas, between the Put what you want to be in there.

[code]

Name:


<p><label>Email: <br />
<input type="text" name="email" class="textfield" value="Email Address" />
</label></p>

<p><label>Message: <br />
<textarea name="message" class="textarea" cols="45" rows="5">Message body</textarea>
</label></p>

<p><input type="submit" name="submit" class="button" value="Submit" /></p>
[/code]

Umm, reading again I’m maybe thinking your wanting the contents of the variable to be put inside the fields?
If thats the case, try this.

[php]<?php
echo(’

Name:


<p><label>Email: <br />
<input type="text" name="email" class="textfield" value="'.$email.'" />
</label></p>

<p><label>Message: <br />
<textarea name="message" class="textarea" cols="45" rows="5">'.$message.'</textarea>
</label></p>

<p><input type="submit" name="submit" class="button" value="Submit" /></p>
'); ?>[/php]

Then reading more, I’m thinking more about first page visit. So you want to set the variables to default if the form hasn’t been submitted yet.

[php] //assign post data to variables
if(isset($_POST[‘name’])){
$name = trim($_POST[‘name’]);
}else{
$name = ‘Insert Name’;
}
if(isset($_POST[‘email’])){
$email = trim($_POST[‘email’]);
}else{
$email = ‘Insert Email’;
}
if(isset($_POST[‘message’])){
$message = trim($_POST[‘message’]);
}else{
$message = ‘Insert message here’;
}[/php]

Hi There,

Thanks for this. It is scenario 3 I was after, i.e. the form fields are filled with name email, message as text, which once the cursor is placed in the fields, this text is removed and the data entered then processed by PHP.

The test site is here:

http://www.veteranscontactpoint.org.uk/test.php

I want to do away with the labels before the form text entry components basically.

Your suggestion for scenario 3 does not work I’m afraid… :frowning:

my Form:

Name:
<?php echo $nameErr; ?>
Email:
<?php echo $emailErr ?>
Message:
<?php echo htmlentities($message); ?>
<?php echo $messageErr ?>
<?php if(isset($_GET['sent'])): ?>
Thank you, your message has been sent
<?php endif; ?>

My PHP:

<?php define("EMAIL", "[email protected]"); if(isset($_POST['submit'])) { include('validate.class.php'); //assign post data to variables $name = trim($_POST['name']); $email = trim($_POST['email']); $message = trim($_POST['message']); //start validating our form $v = new validate(); $v->validateStr($name, "name", 3, 75); $v->validateEmail($email, "email"); $v->validateStr($message, "message", 5, 1000); if(!$v->hasErrors()) { $header = "From: $email\n" . "Reply-To: $email\n"; $subject = "Veterans Contact Point Enquiry"; $email_to = EMAIL; $emailMessage = "Name: " . $name . "\n"; $emailMessage .= "Email: " . $email . "\n\n"; $emailMessage .= $message; //use php's mail function to send the email @mail($email_to, $subject ,$emailMessage ,$header ); //grab the current url, append ?sent=yes to it and then redirect to that url $url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; header('Location: '.$url."?sent=yes"); } else { //set the number of errors message $message_text = $v->errorNumMessage(); //store the errors list in a variable $errors = $v->displayErrors(); //get the individual error messages $nameErr = $v->getError("name"); $emailErr = $v->getError("email"); $messageErr = $v->getError("message"); }//end error check }// end isset ?>

Any help would be great…

[php]<?php
if(isset($_POST[‘name’])){
$name = trim($_POST[‘name’]);
}else{
$name = ‘Insert Name’;
}
if(isset($_POST[‘email’])){
$email = trim($_POST[‘email’]);
}else{
$email = ‘Insert Email’;
}
if(isset($_POST[‘message’])){
$message = trim($_POST[‘message’]);
}else{
$message = ‘Insert message here’;
}
?>

Name:
<?php echo $nameErr; ?>
Email:
<?php echo $emailErr ?>
Message:
<?php echo htmlentities($message); ?>
<?php echo $messageErr ?>
<?php if(isset($_GET['sent'])): ?>
Thank you, your message has been sent
<?php endif; ?> <?php define("EMAIL", "[email protected]"); if(isset($_POST['submit'])) { include('validate.class.php'); //assign post data to variables $name = trim($_POST['name']); $email = trim($_POST['email']); $message = trim($_POST['message']); //start validating our form $v = new validate(); $v->validateStr($name, "name", 3, 75); $v->validateEmail($email, "email"); $v->validateStr($message, "message", 5, 1000); if(!$v->hasErrors()) { $header = "From: $email\n" . "Reply-To: $email\n"; $subject = "Veterans Contact Point Enquiry"; $email_to = EMAIL; $emailMessage = "Name: " . $name . "\n"; $emailMessage .= "Email: " . $email . "\n\n"; $emailMessage .= $message; //use php's mail function to send the email @mail($email_to, $subject ,$emailMessage ,$header ); //grab the current url, append ?sent=yes to it and then redirect to that url $url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; header('Location: '.$url."?sent=yes"); } else { //set the number of errors message $message_text = $v->errorNumMessage(); //store the errors list in a variable $errors = $v->displayErrors(); //get the individual error messages $nameErr = $v->getError("name"); $emailErr = $v->getError("email"); $messageErr = $v->getError("message"); }//end error check }// end isset ?>[/php]

This updates my fields list when I test it out, just I get some errors because of not having functions.
Ofcourse you will have to remove your form labels yourself. But the default values are " Your name, Your email, Your message " then when form is submitted it changes to user submitted information.

When you click on the input fields, instead of deleting the content, you could just select all.

<script> function SelectAll(id) { document.getElementById(id).focus(); document.getElementById(id).select(); } </script> <input type="text" name="name" class="textfield" id="textfield" onClick="SelectAll('textfield');" value="<?php echo htmlentities($name); ?>" />

ahhh - I had to add the PHP to the index page file :stuck_out_tongue:

See - I am a nooooooob :smiley: thanks for that - works great now

No problem.
Add the php to index page, or use include(‘filename.php’); Does the same thing unless you want to use the same code somewhere else :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service