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
Related
If the above title looks confusing then here is the description....
I have a template page where I have placed the wordpress default registration form. Now what exactly I want is to add few extra fields on that form.
The wordpress registration will go on as it is. I mean the username and email and password gets stored on the database but along with that new fields or extra details like phone/address/age etc etc gets emailed to a specific email id.
<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" class="wp-user-form">
<div class="username">
<label for="user_login"><?php _e('Username'); ?>: </label>
<input type="text" name="user_login" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="101" />
</div>
<div class="password">
<label for="user_email"><?php _e('Your Email'); ?>: </label>
<input type="text" name="user_email" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" id="user_email" tabindex="102" />
</div>
<div class="login_fields">
<?php do_action('register_form'); ?>
<input type="submit" name="user-submit" value="<?php _e('Sign up!'); ?>" class="user-submit" tabindex="103" />
<?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
<input type="hidden" name="user-cookie" value="1" />
</div>
</form>
**Note:- The new fields aren't added here.
Is it possible? If yes, then how? Should I add a second form below this form which fires the email? Kindly please suggest an appropriate solution for this.
So I have this code that is supposed to print a bunch of boxed in results, that should function as forms to get any user's data, however when I store a huge html string into a variable to then print onto my html, it only appends the form tags once on one of the boxes. Why is this happening? It does not seem logical to me. This is what I have done with my code:
<?php
if($searchData == "" || $resultArray == null){
echo "<h2>No results found<h2>";
} else {
foreach($resultArray as $iterator){
$finalResult = $finalResult.'<div class="col-lg-4 col-sm-6">
<div class="properties">
<form action="user-detail.php" method="POST">
<h4>'.$iterator['USERNAME'].' </h4>
<h5>'.$iterator['PERSON_NAME'].'</h5>
<h5>'.$iterator['FIRST_LAST_NAME'].'</h5>
<h5>'.$iterator['SECOND_LAST_NAME'].'</h5>
<input class="form-control" type="text" style="display: none" readonly name="username" value="'.$iterator['USERNAME'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="p_id" value="'.$iterator['PERSON_ID'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="name" value="'.$iterator['PERSON_NAME'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="last_name" value="'.$iterator['FIRST_LAST_NAME'].'"/>
<input class="form-control" type="text" style="display: none"readonly name="second_last_name" value="'.$iterator['SECOND_LAST_NAME'].'"/>
<input class="form-control" type="text" style="display: none" readonly name="b_value" value="'.$iterator['BLACKLIST'].'"/>
<input type="submit" class="btn btn-primary" value="View Details"
<form/>
</div>
</div>';
}
echo $finalResult;
}
?>
Normally, I wouldn't ask for help with this, but I have no clue why an error like this could happen. It makes no sense to me as to why this would happen. I would greatly appreciate if someone could clarify as to why this happens.
Your ending form tag is improperly formatted.
You have: <form/>
It should be: </form>
I hope that helps!
This is what my code, problem with all <input> tag, can any one tell me is this the correct way to integrate HTML and PHP
<div style="width:115px; padding-top:10px;float:left;" align="left">Email Address:</div>
<div style="width:300px;float:left;" align="left">
<input type="text" name="email" id="email" value="<?php echo strip_tags($_POST["email"]); ?>" class="vpb_textAreaBoxInputs"></div><br clear="all"><br clear="all">
you need to make sure that $_POST["email"] is set before use for that you can use isset() like
<?php if(isset($_POST["email"])) { echo strip_tags($_POST["email"]); }else{echo 'default' ;} ?>
use ternary operator to display default value for each input instead
<input type="text" name="email" id="email" value="<?php echo isset($_POST['email'])?strip_tags($_POST['email']):''; ?>" class="vpb_textAreaBoxInputs"></div><br clear="all"><br clear="all">
same for firstname
<input type="text" name="firstname" id="firstname" value="<?php echo isset($_POST['firstname'])?strip_tags($_POST['firstname']):''; ?>" >
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
}
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