putting a php variable in a HTML form value - php

I've got a php variable like so.. $name = $_REQUEST['name']; I'd like to put it in a HTML form field's value e.g in here.. <input type="text" name="name" value=(php variable here) />
How would I do so?
Thanks.

value="<?php echo htmlspecialchars($name); ?>"

You can do it like this,
<input type="text" name="name" value="<?php echo $name;?>" />
But seen as you've taken it straight from user input, you want to sanitize it first so that nothing nasty is put into the output of your page.
<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />

Related

HOW to add increment value to $_POST['variable'] in php?

I am using dynamic form where user add more input text boxes for a certain field he want and the name of each box change with an increment like:
<form method="post" action="somescript.php">
<input type="text" name="textbox" />
<input type="text" name="textbox1" />
<input type="text" name="textbox2" />
<input type="text" name="textbox3" />
.... and so on
</form>
I want to echo these data following a loop:
<?PHP
$k=$_POST['counter']; //counter value coming as post variable
for($i=1$i<=$k;$k++){
echo $_POST['textbox'.$i]; //something like this......?
}
?>
Please reply.
Use array notation instead.
<form method="post" action="somescript.php">
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox][" />
.... and so on
</form>
When the form is submitted, $_POST['textbox'] will then be an array, and you can loop over it:
foreach ($_POST['textbox'] as $textbox) {
echo $textbox;
}
I just came across this issue because I had blocks of data that needed to be created dynamically and
echo $_POST["textbox$i"];
worked without the concatenation in it. Let me know if this is bad practice, it works in my situation though. The array way didn't work for me. Sorry for posting this on a 3 year old question. I'm not sure if that's bad practice. Thanks.

read data from text box into php var and then echo

I am new to php and just cant figure how to get the data from a a html textbox and then echo the var.
I am making a registration page, I do everything else but this simple part.
I have a preset value="hi" just for testing if the var populates.
Fyi in the future it will be done after i click a register button. just need to get this.
Thanks all
<input name="fName" id="fName" type="text" value="hi" />
and here is the php which i try to read the data into if the echo is test to check if it populates
<?php
$fName = $_POST['fName'];
//$fName = $_GET['fName'];
echo $fName;
?>
<input type="text" placeholder="NAME" name="name" value="<?php if(isset($_POST['name'])){ echo $_POST['name']; } ?>" />
<input type="text" name="phone" placeholder="PHONE" value="<?php if(isset($_POST['phone'])){ echo $_POST['phone']; } ?>" />
This should work for you:
You have to make a form and submit it! After that you can use the variable$_POST['fName']
<?php
if (isset($_POST['fName'])) //if you also want to check if it is empty use !empty($_POST['fName'])
echo $_POST['fName'];
?>
<form action="" method="post">
<input name="fName" id="fName" type="text" value="hi" />
<input type="submit" name="submit" value="submit!" />
</form>

How to pass a form value to next page in other form

How to pass a form value to next page in other form?
I have this code in the :
<form action="confirm.php" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Email: <input type="text" name="email">
</form>
Now I want that in confirm.php to put some hidden input fields with this values, I tried this code:
<form action="nextpage.php" method="post">
//some other input fields...
<input type="hidden" name="firstname" value="<?php $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php $_POST['email']?>">
</form>
And so on in other 2 pages, and in the last page I want to email all this fields, I tried PHP _SESSION, but no luck with that, so I think that this can be more easier for me!
And something else I forgot to tell, on the second page (nextpage.php) action form variable I refer to a file that use this code:
<?php
header('Location: dear-'.$_POST['firstname'].'.php');
?>
<html>
<form>
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
</html>
In this case how to pass this values (firstname, lastname and email) on the next page ? I use that because I want to generate a page like this www.site.com/dear-name.php
Try this in confirm.php
<form action="nextpage.php" method="post">
//some other input fields...
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
You forgot to echo your variable.
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">
Update:
You have two choices :
Store your data to session and retrieve to another page.
Pass your data using query string.
header('Location: dear-'.$_POST['firstname'].'.php?firstname='.$_POST['firstname'].'&lastname='.$_POST['lastname'].'&email='.$_POST['email']);
use echo:
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']; ?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']; ?>">
and if this still not working, isset() your post variables:
if(isset($_POST['firstname'],$_POST['lastname'],$_POST['email'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
echo '<input type="hidden" name="firstname" value="$firstname">';
echo '<input type="hidden" name="lastname" value="$lastname">';
echo '<input type="hidden" name="email" value="$email">';
}
In your input text field, value is empty because of echo is missing. You need to add echo to print the value in your form. So that it can be sent into another page.
The second option is that you can use session_start() in top of each page.
and store the value of the variable in your session.
$_SESSION['name'] = "value_of_session"// in your case that is POST data
and this can be used in next page you need not to include hidden fields.
the most important thing is that You must write
<?php session_start() ?> // in each(you've three) page.

How to get the Name value using Post method in php

How to get the Name value using Post method in php like this
<input type="text" name="<?php echo $result['name'];?>" id="">
How to Post the Name value..?
<?php
$name=$_POST[''] --? here how can i get the value using name.
Please help me.Thanks
Is this Correct..?
$name=$_POST['$result['name']'];
<input type="text" name="<?php echo $_POST['name'];?>" id="">
this is the wrong way to generate name dynamically... but if you really want this way only then you can do it this way..
<input type="hidden" value=<?php echo $result['name'];?> name="abc"/>
<input type="text" name="<?php echo $result['name'];?>" id="">
don't forgot to check variable is set or not using isset()
in php
if(isset($_POST['abc']))
{
$abc=$_POST['abc'];
$original = $_POST[$abc];
}
Use form tag to post the values from form.
<form action=".." method="post">
<input type="text" name="<?php echo $result['name'];?>" id="name" name="name"/>
<input type="submit" value="submit"/>
</form>
In php to receive the value from form.
$n=$_POST['name'];

retain the values of data entry in a placeholder during postbacks

how to retain the values of data entry in a placeholder during postbacks
<input type="text" name="name" value="<?PHP $name;?>" placeholder="Enter Name"/>
when php found any error and post it back
The data entry are all gone, user have to re enter all the information again.
What does value="<?PHP $name;?>" do? Is this supposed to print the variable? You would need to do this instead:
<?= $name; ?>
or
<?php echo $name;?>
<?php if empty($name){
$name='Enter Name';
} else {}?>
<input type="text" name="name" <?php echo $name;?> placeholder="Enter Name"/>
Try this:
<input type="text" name="name" value="<?PHP echo empty($_POST['name'])?'Enter Name':$_POST['name'];?>" />
In your PHP use your $_POST / $_GET variables to restore your user data:
<input type="text" name="name" value="<?= $_POST['name'] ?>" placeholder="Enter Name"/>
Where $_POST['name'] contains the data sent via a POST request from the form and 'name' is the name of your input.
Try this;
<input type="text" name="name" value="<PHP if (isset($name)){ echo $name; }?>"placeholder="Enter Name"/>
The isset will handle any undefined variable errors, if $name has not been piror to the submission

Categories