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>
Related
I have a tiny issue where sending variables to another page yields undefined variables.
Page 1:
<form name= "updateProfiel" method="POST" action="index.php?page=updateProfiel">
<input required type="text" name="voornaam" value="<?php echo $result['voornaam']; ?>"/>
<input required type="text" name="achternaam" value="<?php echo $result['achternaam']; ?>"/>
<input required type="text" name="adres" value="<?php echo $result['adres']; ?>"/>
<input required type="text" name="postcode" value="<?php echo $result['postcode']; ?>"/>
<input required type="text" name="woonplaats" value="<?php echo $result['woonplaats']; ?>"/>
<input required type="email" name="email" value="<?php echo $result['email']; ?>"/>
<input required type="password" name="password" placeholder="password"/>
<input type="hidden" name="submit" value="true" />
<input type="submit" id="submit" value=" Update " />
Annuleren
</form>
Page 2:
(page name is updateProfiel.php)
$voornaam = htmlspecialchars($_POST["voornaam"]);
$achternaam = htmlspecialchars($_POST["achternaam"]);
$adres = htmlspecialchars($_POST["adres"]);
$postcode = htmlspecialchars($_POST["postcode"]);
$woonplaats = htmlspecialchars($_POST["woonplaats"]);
$email = htmlspecialchars($_POST["email"]);
$password = htmlspecialchars($_POST["password"]);
$password = password_hash($password, PASSWORD_BCRYPT);`
I used phpinfo(); to see if the variables got sent from page 1 to page 2, and they are there:
The phpinfo()
But if I try to set them as variables and use them they're undefined!..
Does anyone know what's happening here?
This is very embarassing.
The undefined variables are actually not undefined. The source-page just says so.
The issue was somewhere else :')... Thanks for everyone's help!
Ps: The issue was the fact that a piece of SQL-query for the database was missing two brackets.. Yay.
I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help
I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.
Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.
Somebody there see something wrong?
<?php
if (isset($_POST['sendForm'])) {
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['date'] = $_POST['date'];
} ?>
<form name="form" action="" method="post">
<label class="line">
<span class="data">Name:</span>
<input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
</label>
<label class="line">
<span class="data">Content:</span>
<textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
</label>
<label class="line">
<span class="data">Date:</span>
<input type="text" name="date" value="<?php if (isset($f['date'])) {
echo $f['date'];
} else {
echo date('d/m/Y H:i:s');
} ?>"/>
</label>
<input type="submit" value="Create" name="sendForm" class="btn"/>
</form>
In short you can set by this way,
<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
You do not need to use $f['name']. you can get value directly by $_POST['name'] method.
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.
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