PHP form post undefined index - php

I have an issue about HTML post to PHP script.
But the values are not posted.
My form.php file codes are;
<form action="http://xxxx/valid.php" method="post">
Name: <input name="Name" value='' type="text" />
Sur Name: <input name="SurName" value='' type="text" />
<input id="submit" type="submit" value="Send" />
</form>
and valid.php codes are;
<?php
echo $_POST["Name"];
echo $_POST["SurName"];
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
die();
?>
I get the blank page and I get this error.
Undefined index: Name Undefined index: SurName
I working on PHP 5.6
What is wrong?
Its Solved !
Changed the http://xxxx/valid.php to /valid.php and its worked.

Try This One.
<form action="http://xxxx/valid.php" method="post">
Name: <input type="text" name="Name" value='' />
Sur Name: <input type="text" name="SurName" value=''/>
<input id="submit" type="submit" value="Send" />
</form>

Try this
$Name = isset($_POST['Name']) ? $_POST['Name'] : '';
$SurName = isset($_POST['SurName']) ? $_POST['SurName'] : '';
echo $Name;
echo $SurName;

try this in valid.php file:
<?php
if(isset($_POST["Name"]) && isset($_POST["SurName"])){
echo $_POST["Name"];
echo $_POST["SurName"];
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
}
die();
?>

I tested your code, all is working fine with me.
if the code is correct then the issue is in the configuration of server or installation (something like that.)
check your configuration maybe it can help...
Well if it's solved then cheers...
happy coding...

Related

I am getting a really weird result on my php form output

Here is my code:
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_POST['postName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$age = $_POST['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_GET['getName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$age = $_GET['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
I have two forms. Both displaying the exact same thing, but one form using POST and one using GET.
I have gotten so close to finishing this off but now I have another small/weird issue.
The code technically works correctly, but here's the output explanation:
when I first open up the page the GET form already has the result "Sorry, try again when you're 16 or older." When I fill out the first 'simple' form, it displays the result correctly but then the POST form shows the "Sorry, try again..." result. Then, when I fill in the information and click submit, it displays the correct result and the other two forms are blank as they're supposed to be, and then the same result when I fill out the GET form.
Any help on this is much appreciated.
Try this code :
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
if (isset($_POST['firstName']) && $_POST['lastName'])
{
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
}
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_POST['postName']))
{
echo $_POST['postName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (isset($_POST['age']))
{
$age = $_POST['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_GET['getName']))
{
echo $_GET['getName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
if (isset($_GET['age']))
{
$age = $_GET ['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
Please try this. I hope it will help.
Replace
if ($_SERVER['REQUEST_METHOD'] == "POST") {
with
if (isset($_POST['age'])) {
Similarly,Replace
if ($_SERVER['REQUEST_METHOD'] == "GET") {
with
if (isset($_GET['age'])) {
When you first enter on page, default REQUEST_METHOD is GET so you should check if isset($_GET['age']) {
and here check if it is more than 16
}
also you should check this one
echo $_GET['getName']; and change on this
echo isset($_GET['getName']) ? $_GET['name'] : "";
You should also check $_POST request like this and your program will work correctly.

500 Server Error AWS Elastic beanstak

Hello Im having an issue with the website I am creating for a project. Everytime I upload it to aws elastic beanstalk it works except when I use a form.
<form action="supply.php" method="POST">
Product Name: <input type="text" name="p_name"><br>
Order Qty: <input type="text" name="ord_qty"><br>
Name: <input type="text" name="user_name"><br>
Address: <input type="text" name="address"><br>
<input type="submit" name="submit" value="Submit"> <input type="reset">
</form>
this is my form and here is my supply.php where I get the 500 error
<html>
<body>
<h1>Thank you for your order. <? php echo $_POST["user_name"]; ?><br></h1>
<form action="order.php">
<input type="submit" value="Go to Orders">
</form>
<?php
$p_name = $_POST['p_name'];
$ord_qty = $_POST['ord_qty'];
$name = $_POST['user_name'];
$address = $_POST['address'];
$string = $p_name. "," . $ord_qty. "," . $name. "," . $address;
$file = "order.txt";
file_put_contents($file,$string . "/n", FILE_APPEND);
?>
</body>
</html>
Is there something wrong? I can't see any issues and its driving me crazy
<? php echo $_POST["user_name"]; ?>
You have a space in the first opening php tag
Try:
<?php echo $_POST["user_name"]; ?>
This was enough to break a simple test page I set up, let me know if you're still experiencing problems.

i keep getting Undefined index: name and Undefined index: email

this is my first form
<style>
.wrap-form{
width: 700px;
min-height: 20px;
background-color: lightblue;
margin: 0 auto;
}
</style>
<div class="wrap-form">
<form method="post" action="advanced-form-send"></form>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</div>
And this is my form 2 where the data will send to
<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo $name;
echo $email;
?>
How would I fix this?
That's how your form code should be, surrounding your submit button by <a href="..." is causing the form not to be submitted, at the place you're just telling the browser that when this button is clicked, take the user to the page advanced-form-search.php. What you should do is put the script name where the form should be submitted in the action of the form tag then just add a submit button. And don't forget to close your tags... you missed the </form>
<div><?php if(isset($_GET['err'])){ if($_GET['err']==1){ echo 'You didn\'t fill in your name'; } elseif($_GET['err']==2){ echo 'You didn\'t fill in a correct email address';}}?></div>
<div class="wrap-form">
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
Here's the PHP code to send an email:
<?php
if(isset($_POST['name']) && !empty(trim($_POST['name']))){
$name = $_POST["name"];
} else {
header("Location: page_where_the_form_is.php?err=1");
die()
}
if(isset($_POST['email']) && filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)){
$email = $_POST["email"];
}
else {
header("Location: page_where_the_form_is.php?err=2");
die()
}
$subject='Form Submitted On The Website';
$message="Name: {$name}\nEmail: {$email}";
mail($to_email, $subject, $message);
?>
You've wrapped your <input type="submit" name="submit"... button with an <a href=.... What this does is prevents your form from being submitted as a POST request, and instead gets linked to as a GET request.
git rid of the <a href..., and and change your action=advanced-form-send to action="advanced-form-send.php".
You can improve your php code:
<?php
if (isset($_POST["name"])) {
$name = $_POST["name"];
echo $name;
} else {
echo 'Eror: Attribute "name" is missing!';
}
if (isset($_POST["email"])) {
$email = $_POST["email"];
echo $email;
} else {
echo 'Eror: Attribute "email" is missing!';
}
?>
And your form should be changed:
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
You need to wrap your variables. The other answers are about the form. This answer is about your handling of variables. PHP will throw a notice if you use a variable that haven't been set. By using isset() you can determine if the variable is set and then use it.
<?php
$name = isset($_POST["name"]) ? $_POST["name"] : '';
$email = isset($_POST["email"]) ? $_POST["email"] : '';
echo $name;
echo $email;
?>
Doing it this way ensures that you wont get a notice if one of your fields haven't been posted. If they have you will have the value in $name and $email. If not then the variables will be empty strings.
Try Like this,
<div class="wrap-form">
<form method="post" action="advanced-form-send">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
The submit button should be in the two "form" markups. Also the link is unnecessary because the input "submit" will send the form with the data to the page you set in the attribute "action" of the form markup.
<div class="wrap-form">
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
Youre missing an enctype ... you ALL forgot it. Shame above you!
<form method="post" action="advanced-form-send.php" enctype="multipart/form-data"></form>

Display the input form

I have code in html for a form which needs to be filled. When the button 'OK' is clicked, values are sent to a php script. I use $_POST. Can I display the same form when input is not of the right format but do this only inside my php script?
This is where I check some of my fields but I don't know how to re-display the form.
if (isset($_POST["name"])) {
$name = $_POST["name"];
}
if (isset($_POST["date"])) {
$date = $_POST["date"];
}
Thanks a lot.
You could try something like this:
<?php
foreach($_POST as $key => $value) {
$$key = $value;
}
?>
<form method="post" action="">
<input type="text" name="name" value="<?php echo !empty($name) ? $name : 'Fill in your name'; ?>" />
<input type="text" name="age" value="<?php echo !empty($age) ? $age : 'Fill in your age'; ?>" />
<input type="text" name="what" value="<?php echo !empty($what) ? $what : 'what'; ?>" />
<input type="text" name="ever" value="<?php echo !empty($ever) ? $ever : 'ever'; ?>" />
<input type="submit" value="Go" />
</form>
If you are looking for a php template engine to split the PHP and HTML, I recommand Smarty
EDIT
To split the HTML and PHP without an engine, you could do something like combine the functions file_get_contents() and str_replace like here:
Template.html
<form method="post" action="">
<input type="text" name="name" value="#_name_#" />
<input type="submit" value="Go" />
</form>
PHP
<?php
$template = file_get_contents('template.html');
foreach($_POST as $key => $value) {
$template = str_replace('#_'.$key.'_#', !empty($value) ? $value : '');
}
echo $template;
?>
That way you get the .html file, and replace #_name_# with the post or a default value.
Still I recommand you to use Smarty

Get variable from query string

I was trying to get variable in Query String from URL. But somehow, its just got one variable instead of getting all variables from querystring. I really don't know what goes wrong with my code. Here is the code I want to print out error from the invalidate form:
<?php
displayForm();
function displayForm(){
?>
<form action="./prod_add_action.php" method="post" name="addproductForm">
<fieldset>
<legend>Online Ordering System Setup</legend>
<label for="product_name">Product Name: </label><input type="text" name="product_name" value="" /><?php echo $_GET["name_error"]; ?>
<label for="product_date">Product Date: </label><input type="text" name="product_date" value="" /><?php echo $_GET["date_error"]; ?>
<label for="product_price">Product Price: </label><input type="text" name="product_price" value="" /><?php echo $_GET["price_error"]; ?>
<input name="add_button" type="submit" value="Add" />
<input name="reset_button" type="reset" value="Clear" />
</fieldset>
</form>
<?php
}
?>
And here is the code I created the querystring:
$query_string = "name_error=" .urlencode($name_error) ."&date_error=" .urlencode($date_error) ."&price_error=" .urlencode($price_error);
header("Location: ./prod_add.php?$query_string");
exit();
In the first code, the page only print the first $_GET['name_error'], while it should be include $_GET['date_error'] and $_GET['price_error. ']
This is the address:
http://example.com/prod_add.php?name_error=Product+name+must+be+characters+only&date_error=Product+date+must+be+input+as+this+formate+DD-MM-YYYY&price_error=Product+price+must+be+float+number+only
You should use & instead of &'s ?
$query_string = "name_error=" .urlencode($name_error) ."&date_error=" .urlencode($date_error) ."&price_error=" .urlencode($price_error);
header("Location: ./prod_add.php?$query_string");
exit();
Change & to & as:
$query_string = "name_error=" . urlencode($name_error) . "&date_error=" . urlencode($date_error) . "&price_error=" . urlencode($price_error);
header("Location: ./prod_add.php?$query_string");
exit();

Categories