Ik want to prevent field reset after validation. I have searched some topics but i cant implement it in my code.
if($form_view == true) {
echo '';
if($error != '') {
echo '<strong><font color="#FF0000">Fout:</font></strong><br />'.$error.'<br />';
}
echo '<form method="POST" action="'.$filename.'" style="border:0px; margin:0px; padding:0px;">
Voornaam
<br />
<input type="text" name="voornaam" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['voornaam']) ? $_POST['voornaam'] : '').'">
<br />
Achteraam
<br />
<input type="text" name="achternaam" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['achternaam']) ? $_POST['achternaam'] : '').'">
<br />
Adres
<br />
<input type="text" name="adres" maxlength="50" id="input_contact" style="width: 400px; value="'.(isset($_POST['adres']) ? $_POST['adres'] : '').'">
<br />
Postcode <h7><i><small>(1234 AB)</small></i></h7>
<br />
<input type="text" name="postcode" maxlength="7" id="input_contact" style="width: 100px; value="'.(isset($_POST['postcode']) ? $_POST['postcode'] : '').'">
<br />
Woonplaats
<br />
<input type="text" name="woonplaats" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['woonplaats']) ? $_POST['woonplaats'] : '').'">
<br />
Telefoonnummer <h7><i><small>(0123-456789)</small></i></h7>
<br />
<input type="text" name="telefoonnummer" maxlength="11" id="input_contact" style="width: 100px; value="'.(isset($_POST['telefoonnummer']) ? $_POST['telefoonnummer'] : '').'">
<br /><br />
If you can give me one example on how to implement this in one field.
Thanks a lot
If the post is not made to this specific page they're landing on then the $_POST variable will not contain that information.
The quick and dirty of it is to save the information submitted in the $_SESSION[] as an array.
When the form is submitted, in addition to doing whatever you're currently doing, you need to save the information submitted into the session on the receiving script.
if ($_POST['submit']) {
// repeat or configure as desired to save submitted fields into Session
$_SESSION['form_info']['email_address'] = $_POST['email_address'];
}
On the form page itself you would use the following
if (isset($_SESSION['form_info'])) {
// You'll want to most likely consider filtering these using appropriate functions.
<input type="text" name="email_address" maxlength="50" id="email_address" style="width: 200px; value="<?php if (isset($_SESSION['form_info']['email_address']) { $_SESSION['form_info']['email_address']; } ?>">
}
Is the form action variable $filename referring to this particular php file or somewhere else where validation happens? If the form action happens somewhere else, the Moylin's answer is your solution (using $_SESSION). Otherwise, if possible, it's reasonable and simple to do the validation right here. echoing $_POST to value fields, as you did, should be sufficient enough (you dont even need the ternary operation there, if you want short code).
<form method="POST" action="thisPhpFile.php" >
value ="'. #$_POST['voornaam'] .'"
In the validation part (before echoing the form), you naturally want to check for all the errors, required fields etc. If everything is valid just point user to wanted location. Something along these lines:
if(isset($_POST['mySubmit'] {
//check errors/validate
if($valid == true) {
Header("Location: yourLocation.php");
}
}
else {
//echo your form here
}
Related
I have a PHP form and need to pre-fill one of the fields with some static free text, for this exercise let's call it "MYTEXT".
if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['jobtitle']))
{
$jt = mysqli_real_escape_string ($dbc, $trimmed['jobtitle']);
}
else
{
echo '<p style="font-weight: bold; color: #C00">Please enter where you heard about us!</p>';
}
And the form:
<tr>
<td class="single-field-left">
<div class="field">
<label for="jobtitle">Your job title<em class="red">* </em></label>
<div class="input-box">
<input class="input-text" name="jobtitle" title="jobtitle" type="text" value="<?php if (isset($trimmed['jobtitle'])) echo $trimmed['jobtitle']; ?>" id="jobtitle" />
</div>
</div>
</td>
</tr>
Instead of the input box being empty I need it to have "MYTEXT" in the field as soon as the page loads. It needs to be actual text, not just a placeholder.
I'm a PHP novice so guidance appreciated
Your question is not that much clear. Anyways i think your value is stored in the variable $jt. Try the code below :)
<input class="input-text" name="jobtitle" title="jobtitle" type="text" value="<?php if (isset($jt)){ echo $jt; }else{echo "";} ?>" id="jobtitle" />
<input class="input-text" name="jobtitle" title="jobtitle" type="text" value="MYTEXT" id="jobtitle" />
Thanks to hd for the answer
HD's fiddle
i want to update value(s) as a student change value(s) in correction or update form.
till know i'm able to fetch and display values in text boxes on the bases of name selected from dropdown list from data base using ajax and json. but when i try to update database it do not works...
HTML:
<select name="u_stu" id="u_stu" onchange="show(this.value);" style="float:right; height:30px; width:180px;">
<option selected="selected" disabled="disabled">Choose</option>
<option>stu1</option>
<option>stu2</option>
<option>stu3</option>
</select>
name: <input type="text" id="name" name="name" style="float:right; height:20px; width:200px;"/><br /><br />
age: <input type="text" id="age" name="age" style="float:right; height:20px; width:200px;" /><br /><br />
phone: <input type="text" id="phone" name="u_ver_txt" style="float:right; height:20px; width:200px;" /><br /><br />
address: <input type="text" id="add" name="add" style="float:right; height:20px; width:200px;" /><br /><br />
hobby: <input type="text" id="hobby" name="hobby" style="float:right; height:20px; width:200px;" /><br /><br />
<input type="submit" value="Submit" name="u_s2" id="u_s2" style="position:relative; top:-180px; "/>
MYSQL PHP
<?php
$c=mysql_connect("localhost","abc","xyz");
mysql_select_db("root");
if(isset($_POST['u_s2']))
{
$name=$_POST["name"];
$age=$_POST["age"];
$phone=$_POST["phone"];
$address=$_POST["address"];
$hobby=$_POST["hoddy"];
$id=$_POST["u_id"];
$q2="UPDATE student SET
name=$name,age=$age,phone=$phone,address=$address,hobby=$hobby WHERE Sr. no=$id";
mysql_query($q2);
}
?>
You need something like this:
$q= "update student set name = '".$name."', age = '".$age."', phone = '".$phone."', address = '".$address."', hobby = '".$hobby."' WHERE user = 'the user id'";
You should use a WHERE statement as well like the above example so that you can be sure that you are updating the correct row.
You should also consider using mysql_real_escape_string function:
$name = mysql_real_escape_string($_POST['name']);
http://php.net/manual/en/function.mysql-real-escape-string.php
If this will be an updating form you should also include the value in the input fields and etc. so they will see the values and update what they need.
I also suggest that you use mysqli functions instead of mysql functions as mysql is no longer supported and deprecated.
I'm trying to cache the user input in my form. My code is:
echo '
<form method="POST">
Name:<br /><input class="contact_name" type="text" name="contact_name" maxlength="32" placeholder="Enter Name" /><br />
Email:<br /><input class="contact_email" type="text" name="contact_email" maxlength="50" placeholder="Email Address" /><br />
Subject:<br /><input class="contact_subject" type="text" name="contact_subject" maxlength="50" placeholder="Subject Line" /><br />
Message:<br /><textarea class="message_area" name="contact_message" rows="10" cols="50" maxlength="1000" placeholder="Message ..." /></textarea><br />
<input class="submit_button" name="submit_button" type="submit" value="Send" />
</form>
';
I tried searching for the answer and the only thing I found was adding:
<?php if(isset($contact_name)) { echo $contact_name; } ?>
This however does not work for me as my form is within a PHP echo and I'm trying to make a basic wordpress plugin. Whenever I bring the form outside the echo and , the style messes up and the form style itself breaks. So I was wondering if I can keep my form inside my echo along with a placeholder and be able to cache user inputs so when an error displays cause they didn't fill one of the spots out, it won't erase everything.
Thank you.
Then, just drop the echo and switch to HTML mode:
?>
<form method="post">
Name:<br /><input ... value="<?php echo (isset($contact_name) ? htmlspecialchars($contact_name, ENT_QUOTES, 'UTF-8') : ''; ?>" />
...
<?php
If you need this as a string, you could use output buffering:
ob_start();
?>
<input ... />
<?php
echo ob_get_clean();
if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one
First of all thanks in advance, this has been very frustrating and I'm hoping someone can see something I'm not, I am definitely no php expert. Well here' what is going on.
I have a form where I have a checkbox for people to opt in to our newletter. The form element looks like this:
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
That is then submitted to a php file with this code:
if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes'){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
$newsletter is then inserted into a database field.
The issue is that whether the box is checked or not it is being sent to php as true, so every entry is receiving the newsletter.
Any help would be greatly appreciated! Thanks!
Here's the full form minus the option list for the sake of brevity
<form method="post" action="contact.php" name="contactform" id="contactform">
<fieldset>
<legend>Please fill in the following form all fields are required, thanks!</legend>
<label for=firstName accesskey=F><span class="required">*</span>First Name</label>
<input name="firstName" type="text" id="firstName" size="30" value="" />
<br />
<label for=lastName accesskey=L><span class="required">*</span>Last Name</label>
<input name="lastName" type="text" id="lastName" size="30" value="" />
<br />
<label for=email accesskey=E><span class="required">*</span>Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<br />
<label for=city accesskey=C><span class="required">*</span>City</label>
<input name="city" type="text" id="city" size="30" value="" />
<br />
<label for=state accesskey=S><span class="required">*</span>State</label>
<select name="state" type="text" id="state">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
<br />
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
<br />
<p><span class="required">*</span> Are you human?</p>
<label for=verify accesskey=V> 3 + 1 =</label>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
Your code is correct. You most likely have a problem with your database insert/update logic. Why do you assume it is the PHP form handling?
This has just dawned on me.
If a checkbox is unchecked it isn't set in the $_POST superglobal. So if !isset($_POST['newsletter']) then it wasn't checked - if isset($_POST['newsletter']) it was checked.
Edit: Remove the 'yes' part - the value will never be yes, just true or 'on'.
Edit 2:
I've tested this to death. Change your code to:
if (isset($_POST['newsletter'])){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
Remove the value="Yes" attribute from your checkbox input also. If you want it checking by default use checked="checked".