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.
Related
<?php
Function runSearch($name)
{
If(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
This code is suppose to display what is entered into the Search String text box. When I don't use a function it works fine. But as soon as I place the code into the function runSearch there is no output. I'm new to php can an argument be sent to a php function and then displayed on the screen?
you need to call your function, otherwise nothing will happen. Also you need to removed the $name-parameter:
<?php
function runSearch()
{
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
runSearch();
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
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...
I need to auto fill fill the CITY: form when typing in the zip code,
it says "undefiened variable: array on line.." where value="< ? php $array['navn'] ">
can someone help ?
http://i62.tinypic.com/hv5fkl.jpg
<div id="search">
<form name="input" action="" method="get">
POST:<input type="text" name="postcode"><br><br>
City:<input type="text" name="navn" value="<?php $array['navn'] ?>"><br><br>
<input type="submit" value="search">
</form>
</div>
</div>
<?php
if(isset($_GET['postcode'])){
$postcode = $_GET['postcode'];
$content = file_get_contents('http://oiorest.dk/danmark/postdistrikter/'. $postcode . '.json');
$array = json_decode($content, true);
echo $array['navn'];
echo "<br>";
}
?>
You'd want to always initialize variables. What happens is that you're trying to access a variable that hasn't been initialized yet.
<?php
$city = ''; // initialize containers
$postcode = '';
if(isset($_GET['postcode'])){
$postcode = $_GET['postcode'];
$content = file_get_contents('http://oiorest.dk/danmark/postdistrikter/'. $postcode . '.json');
$array = json_decode($content, true);
// when the request is made, then assign
$city = $array['navn'];
}
?>
<!-- so that when you echo, you'll never worry about undefined indices -->
<div id="search">
<form name="input" action="" method="get">
POST:<input type="text" name="postcode" value="<?php echo $postcode; ?>"><br><br>
City:<input type="text" name="navn" value="<?php echo $city; ?>"><br><br>
<input type="submit" value="search">
</form>
</div>
In this answer, what happens is that, upon first load (no json request yet), the values are empty but they are declared on top.
When you submit the form, then that variable assignment happens and substitutes the values into that container.
Simple Demo
I have a code of a form that writes the "email" of user into a .txt file in my server.
Here are some things I want to do: Make the form have more than one variable (Like, one line to "name" and another one to "email"), put the form writing in .txt prefixes before the "input" texts, and make the output .txt file have line breaks between the contents of each variable.
Here is my code currently
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
fclose($file);
print_r(error_get_last());
}
?>
<form action= "" method="post" name="form">
Email:
<input type="email" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit">
<br>
</form>
Can you help me? Thank you all.
You can add another input to your form. In your code, you have <input type="email" name="email"> for the email address. Just add a <input type="text" name="name"> for the name. The field's name (name="xxx") is the key that you can use in PHP in $_POST['xxx'] to get the information for another line.
Did I understand your question "How can I put the form writting in .txt prefixes before the "input" texts?" correct that you want to have an output in your file like "Email: x#x.x"?
To add a line break, you can write "\n" (if you use a Windows System, use "\r\n") in your strings.
Instead of fopen(), fwrite() and fclose() you can use file_put_contents() which makes your code a bit easier to read.
You can try this code:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$new_content = "\r\nName: " . $name;
$new_content .= "\r\nEmail: " . $email;
file_put_contents('emaillist.txt', $new_content, FILE_APPEND);
print_r(error_get_last());
}
?>
<form action="" method="post" name="form">
Name:
<input type="text" name="name"><br>
Email:
<input type="email" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
Please try with following code .
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$name=$_POST['name'];
$variable = $name ." ". $email. PHP_EOL;
$file = fopen("emaillist.txt","a+");
fwrite($file,$variable);
fclose($file);
print_r(error_get_last());
}
?>
<form action= "" method="post" name="form">
Email:
<input type="email" name="email">
<br>
Name:
<input type="name" name="name">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
Im unable to find out the solution , please help. below is the code. Thanks in advance
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$Lname = $_POST['lastName'];
$num = $_POST['number'];
echo $name.$Lname.$num;
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><Person></Person>");
Header('Content-type:text/xml');
$name = $xml->addChild('name',$name);
$Lname = $xml->addChild('LastName',$Lname);
$Number = $xml->addChild('Number',$num);
print($xml->asXML());
}
?>
<!DOCTYPE html>
<head>
<title>XML</title>
</head>
<body>
<form method="post" action="" enctype= multipart/form-data>
<input type="text" name="name" value="Name" required/>
<input type="text" name="lastName" value="LName" required/>
<input type="text" name="number" value="Number" required/>
<input type="submit" value="send" name="submit"/>
</form>
</body>
</html>
Im unable to find out the solution , please help. below is the code. Thanks in advance
You need to wrap the <form> in the else part and remove the unneccesary echo statement.
The working code..
<?php
if(isset($_POST['submit'])){
header('Content-type:text/xml');
$name = $_POST['name'];
$Lname = $_POST['lastName'];
$num = $_POST['number'];
//echo $name.$Lname.$num; //<---- Commented
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><Person></Person>");
$name = $xml->addChild('name',$name);
$Lname = $xml->addChild('LastName',$Lname);
$Number = $xml->addChild('Number',$num);
print($xml->asXML());
}
else
{
?>
<!DOCTYPE html>
<head>
<title>XML</title>
</head>
<body>
<form method="post" action="" enctype= multipart/form-data>
<input type="text" name="name" value="Name" required/>
<input type="text" name="lastName" value="LName" required/>
<input type="text" name="number" value="Number" required/>
<input type="submit" value="send" name="submit"/>
</form>
</body>
</html>
<?php } ?>
OUTPUT :
<Person>
<name>Nameasdasd</name>
<LastName>LNameasdasd</LastName>
<Number>Number32424</Number>
</Person>
You can not mix XML and HTML in that way. It invalidates the XML document.
Remove:
echo $name.$Lname.$num;
Ref. prolog. This is what causes the error you witness.
Abort script on form data (or use an else clause):
print($xml->asXML());
exit(0);
Else you get data in trailing section of XML-document.
For valid HTML you also need to add a value for action in the form. Quotes on attribute values are usually optional, but would advice consistency, thus also quote enctype.
<form method="post" action="" enctype= multipart/form-data>
replace it with
<form method="post" action="" enctype="multipart/form-data">