Getting error in echoing value of POST - php

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']):''; ?>" >

Related

pass value from $_GET to $_POST

So, I have a disabled email form field whose placeholder and value are retrieved from $_GET['email']:
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" disabled>
When the user fills out the form, I was hoping that $_POST['email'] would have the email value, but it doesn't (it's empty). What am I missing/forgetting? Is there a clever way to pass this value along? Thanks!
change attribute disabled to readonly because disabled not submit values..
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" readonly>
As Cuchu stated, you could use readonly instead of disabled. Or you could duplicate the field and change the type to hidden.
<form method="post" action="register.php">
<input type="text" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" disabled>
<input name="email" type="hidden" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>">
</form>
I think it is better to use the readonly attribute instead of disabling the input.
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" readonly="readonly">
Placeholder is a prompt, not a value. If you want the textfield to have a value of the email, use the "value" attribute, not the "placeholder".
You should enclose in a form tag and set the method to post as
<form action="" method="post">
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" disabled>
</form>
//try this
<form method="post">
<input type="text" name="email"><input type="submit" value="click" name="btnClick" id="btnClick">
<input type="text" name="email1" placeholder="inserted value" value="<?php echo (isset($_POST['email']))? $_POST['email'] : "" ?> ">
</form>

Keep form values after post <?php>

I want to keep form values after submit. I found some methods something like that :
However; when I run the html file, first : the code doesn't work, second: php codes can be seen in text areas. What should I do. Thank you for reading.
<form method="post" action="mail.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" value="<?php if (isset($_POST['Name'])){echo htmlentities($_POST['Name']); }?>" />
<label for="Subject">Subject:</label>
<input type="text" name="Subject" id="Subject" value="<?php if (isset($_POST['Subject'])){echo htmlentities($_POST['Subject']); }?>"/>
<label for="Phone">Phone:</label>
<input type="text" name="Phone" id="Phone" value="<?php if (isset($_POST['Phone'])){echo htmlentities($_POST['Phone']); }?>"/>
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" value="<?php if (isset($_POST['Email'])){echo htmlentities($_POST['Email']); }?>"/>
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message">
<?php if (isset($_POST['Message'])){echo htmlentities($_POST['Message']); }?>
</textarea>
<input type="submit" name="submit" value="Send" class="submit-button" />
</form>
It's not a php page, it's html, change the extension to .php so the browser knows it's got to read some php code.
Your POSTed values will only exist once the form has been submitted, so will initially not exist. If you want 'default' values in your form fields, you need to change the code inside your value='' elements to something like:
<?php echo (isset($_POST['Name'])) ? htmlentities($_POST['Name']) : 'Default text' ?>

Pass PHP session variable into HTML form

I have session variables that are posted from a form on another php page, and I can echo them by using:
<?php $_SESSION['newsletterSignup'] = $_POST['newsletterSignup'];
echo "Email = ". $_SESSION['newsletterSignup'];
?>
But I can't seem to insert these into a HTML form field:
<input type="text" name="email" id="email" size="36" "value="<?php echo $_SESSION['newsletterSignup'];?>" class="text-input" onBlur="emailval()" />
This should work...the quote before value was probably screwing it up
<input type="text" name="email" id="email" size="36" value="<?php echo $_SESSION['newsletterSignup'];?>" class="text-input" onBlur="emailval()" />
you use
<input type="text" name="email" id="email" size="36" "value="<?php echo $_SESSION['newsletterSignup'];?>" class="text-input" onBlur="emailval()" />
A " to many. Here is the fix
<input type="text" name="email" id="email" size="36" value="<?php echo $_SESSION['newsletterSignup'];?>" class="text-input" onBlur="emailval()" />
Also, did you use session_start(); ?

PHP validation resets the form fields

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

CodeIgniter form verification and class

I'm using the form validation library and have something like this in the view
<p>
<label for="NAME">Name <span class="required">*</span></label>
<?php echo form_error('NAME'); ?>
<br /><input id="NAME" type="text" name="NAME" class="" value="<?php echo set_value('NAME'); ?>" />
</p>
I'd like to add a class to the input that is dependent on the form error
so i could have something like this
<input id="NAME" type="text" name="NAME" class="<?php echo $error;?>" value="<?php echo set_value('NAME'); ?>"
I realize that it would have to be a bit more complicated, but I'd like to this this without creating a custom rule callback for every field.
Thanks
~Daniel
If you set all your validation fields in your controller using:
$this->validation->set_fields()
You will have access to any possible error messages, which you could use in the following manner:
<input id="NAME" name="NAME" value="<?php echo $this->validation->NAME; ?>" <?php echo (!empty($this->validation->NAME_error)) ? 'class="error"' : ''; ?> />
http://codeigniter.com/user_guide/libraries/validation.html
As pointed out in the comments, the code above references the old validation library. The new 1.7 way to perform this task would be:
<input id="NAME" name="NAME" value="<?php echo set_value('NAME'); ?>" <?php echo (!empty(form_error('NAME')) ? 'class="error"' : ''; ?> />
I just realized, there is a way to do this with error delimiters
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
I this makes it much easier.
But just out of curiosity, Is there any way of doing this with class in the input?

Categories