I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];
Related
I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .
I'm trying to get a value from a form, then display it on posting of the form. I can get the value to appear in the second text field, once I have chosen an option using the Ajax Auto-Select, but how do I get that value shown stored into a variable for display on posting? This is what I have been trying -
if ($_POST['action'] == 'getentity') {
$value= $entity;
$content .= '<div>'.$value.' hello</div>';
}
<form method="post" action="?">
<input type="text" name="TownID_display" size="50" onkeyup="javascript:ajax_showOptions(this,\'getEntitiesByLetters\',event)">
<input type="text" name="TownID" id="TownID_display_hidden" value="'.$entity.'" />
<input type="hidden" name="action" value="getentity" />
<input type="submit" name="submit" value="Find"/>
Many thanks for any help.
Try
<input type="text" name="TownID" id="TownID_display_hidden" value="<?php $value = $entity; echo $entity; ?>" />
and its better to use like this
if ($_POST['action'] == 'getentity') {
$value= $_POST['TownID'];
$content .= '<div>'.$value.' hello</div>';
}
it should work.
I cannot figure out how to go about keeping the data saved on $myArray after hitting Save. Everytime i do that, it replaces the other input.
I need to be able to save all user enters and save in text file. basically keep saving data on array and update file as well.
Any suggestions?
<!DOCTYPE>
<html>
<body>
<form action="list.php" method="POST">
Name: <input type="text" name="name[]" /><br />
Email: <input type="text" name="name[]" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
$myArray = array();
if (isset($_POST['save']))
{
array_push($myArray,$_POST['name']);
}
print_r($myArray);
?>
</body>
</html>
Every time your user hits Submit, there is a new request sent to your script.
The variable $myArray is created on the first line of your PHP code, and is an empty array.
Variables do not persist across requests. I suggest you use either a cookie, or a session variable for this.
Example of the latter is here:
<?php
session_start(); // This has to be done before any data is sent to the client
session_regenerate_id(); // Good practice to prevent session hijacking
?>
<!DOCTYPE>
<html>
<body>
<form action="list.php" method="POST">
Name: <input type="text" name="name[]" /><br />
Email: <input type="text" name="name[]" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
if( !isset( $_SESSION['myArray'] ) || !is_array( $_SESSION['myArray'] ) )
{
$_SESSION['myArray'] = array();
}
if (isset($_POST['save']))
{
$_SESSION['myArray'][] = $_POST['name'];
}
print_r($_SESSION['myArray']);
// Here you can write the contents of $_SESSION['myArray'] to a file.
?>
</body>
</html>
This way you will keep track of it in the text file:
<html>
<body>
<form action="list.php" method="POST">
Name: <input type="text" name="name[]" /><br />
Email: <input type="text" name="name[]" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
$myArray = array();
if (isset($_POST['save']))
{
$f = file_get_contents("myfile.txt");
$myArray = split($f, "\n");
array_push($myArray,$_POST['name']);
file_put_contents("myfile.txt", implode($myArray, "\n"));
}
print_r($myArray);
?>
</body>
</html>
(You may want to use PHP_EOL [or whatever that constant is - I always have to look it up] in place of "\n")
Since HTML is by nature transactional you do not keep track of ANYTHING except sessions, databases, files - any variables you have start over empty each time you load your page again.
Sorry if this is a rather basic question.
I have a page with an HTML form. The code looks like this:
<form action="submit.php" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input type="submit" />
</form>
Then in my file submit.php, I have the following:
<?php
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
?>
However, I want to eliminate the use of the external file. I want the $_POST variables on the same page. How would I do this?
Put this on a php file:
<?php
if (isset($_POST['submit'])) {
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
}
?>
<form action="" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit'] won't be set because the form has not been sent.
Once you click on the submit button, it will print the information.
I have the following code in a php template called contact_us. I have created a new page which uses this template, but when you click submit it doesn't post back to the same page and display what the user entered in the form. Any ideas why this doesn't work?
Thanks,
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["name"];
$comments = $_POST["comments"];
echo $name;
echo $comments;
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post" >
Name : <br/>
<input type="text" name="name" /><br/>
Comment <br/>
<textarea name="comments" name="comments"></textarea>
<br/><br/>
<input name="submit" type="submit" id="submit" value="Send" />
</form>
Make sure that you don't use "name" as a variable name. I will assume that the same thing goes for comments.
More information here
http://wpquicktips.wordpress.com/2010/02/17/use-an-empty-action-attribute-in-forms/
Does it make any difference removing <?php echo $PHP_SELF;?> from the action and leaving it blank?
I managed to get this to work by adding an id attribute to the form. I think this is something wordpress requires.