i tried to do some practise about form actions and inputs and copy-paste a piece of codes but i got Undefined index: fname and Undefined index: age. The codes were taken from very popular php tutorial website, there should not be any problem with them but submitting them anyway.Is this server error or something like that?
<form action="posting.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
<?php
echo $_POST["fname"];
echo $_POST["age"];
?>
You need to make sure $_POST is getting information before you try to read from it. There are a variety of ways to do this. Here's one:
<form action="posting.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
<?php
if(isset($_POST)) {
echo $_POST["fname"];
echo $_POST["age"];
}
?>
Until that form is submitted, $_POST is empty and trying to read anything out of it will result in such a notice.
<?php
if (isset($_POST['fname']) && isset($_POST['age'])) {
echo $_POST['fname'];
echo $_POST['age'];
}
Related
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 am a completely new programmer and I am trying to develop a website in php. All I want to do in this part of the code is to "read" the user's inputs and save them as session variables in order to do/ calculate something else in another subpage. In order to see if everything is working fine I added the lines echo "Welcome ",$_SESSION["firstname"]; echo "ok" but it is not working. Can you help me?
Here is my code:
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>
</br></br>
<input type="submit" name="submit" value="Submit">
</form
<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
echo "Welcome ".$_SESSION["firstname"];//here is....
echo "ok"
?>
</body>
</html>
What session_start() does is sends a cookie in the page header when it's served to the browser. If you've already send some data to the browser, like your form, then PHP will not be able to start a session. You need to move session_atart() to the very top of your document, above the form or any echos. Also, you're missing a semi-colon, as noted by others. Also, you need to properly close your <form> tag.
That is actually working. If you look at the source code you will see the your name is echoed out.
One issue is you are missing a greater than sign at the end of your form tag.
</form
should be
</form>
Also, you need to start the session before any html. Try this...
<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>
</br></br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_SESSION["firstname"] != ""){
echo "Welcome ",$_SESSION["firstname"];
echo "ok";
}
?>
</body>
</html>
echo output inside if, delcare session start on top, tested and works 100%. Note- add php code on top and close form tag
<?php
session_start(); //session start must be first line
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
echo "Welcome ".$_SESSION["firstname"]; //inside if condition
echo "ok";
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="#">
<b>First Name:</b> <input name="firstname" type="text" value=""/></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""/></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""/></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""/></br></br>
</br></br>
<input type="submit" name="submit" value="Submit"/>
</form> <!-- close form tag -->
</body>
</html>
I'm just learning about html and php but it doesn't work. Could anyone point me in the right direction:
My form:
<form action="" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
PHP:
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int) $_POST['age']; ?> years old.
When I click submit the page loads to the same page but it doesn't pick up either name or email.
I've also tried adding:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
But when I click submit it brings up a 404 error with:
<?php%20echo%20htmlspecialchars($_SERVER[
in the last bit of my URL?
Any help would be highly appreciated.
PHP:
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.
In input type of age write:
<input type='number' ....
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>
<html>
<body>
<form action="" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
<?php
if( $_GET["name"] || $_GET["age"] )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
}
?>
Question:
when I open above script in browser, It shows:Notice: Undefined index: name in D:\wamp\www\oop\test3.php on line 13,
I know if is because the form is not submit yet, so $_GET["name"] does not exist, but how to fix this problem?
Just set the name of the submit button, and use isset() to check if the form was submitted.
<html>
<body>
<form action="" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
</body>
</html>
<?php
if( isset($_GET['submit']) )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
}
?>
The example below will check if both fields are filled.
If one or the other is not filled, an error message will appear.
If both are filled, then the user will see:
Welcome Bob // assuming the person's name is "Bob" in the field.
You are 30 years old. // assuming the person entered "30" in the field.
Otherwise, it will echo:
Fill in all fields.
Here is the example below:
<html>
<body>
<form action="" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
</body>
</html>
<?php
if($_GET['name'] && $_GET['age'])
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
}
if(!$_GET['name'] || !$_GET['age'])
{
echo "Fill in all fields";
}
?>
I've made use of both && and || logical operators for validation.
Consult the PHP manual for more information: http://php.net/manual/en/language.operators.logical.php
You can test if your particular _GET variable exists. For example:
<?php
if ( isset ( $_GET["name"] ) ) {
echo "Welcome ". $_GET['name']. "<br />";
}
?>
Try:
<form action="" method="GET">
Name: <input type="text" name="name" id="name" />
Age: <input type="text" name="age" id="age" />
<input type="submit" />
</form>