When submitting a web form, don't reset data fields - php

How can I submit a form to itself without clearing the data in the fields using HTML, javascript and PHP?

You could take different approaches (e.g. cookies, jquery, etc...), however HTML + a line in PHP are more than enough in this case. Try this example code:
<form name="test" method="post">
Your Name: <input type="text" name="YourName" <?php if (isset($_POST['YourName'])) echo 'value="'.$_POST['YourName'].'"';?> >
<input type="submit" value="Submit">
</form>
In the code above if something has been posted to the receiving page (that can be the same page, such as in your case), then the posted value is printed out in the corresponding field. You can use this approach for all the fields composing your form.
If you want, you can also use similarly the $_GET method in the form.

If you use the traditional form submit, you need to save the parameters and rewrite the form input elements when you write the form the next time. But a better way is to use AJAX -- then the field data is sent without a form submission, and the input elements retain their data. See this link: http://www.w3schools.com/ajax/default.asp

Related

form type="url" POST to PHP

So I have a form that requires a user to submit their website to a form. Here is the html line:
<input type='url' name='link'>
And I'm using <input type="submit" value="submit" formmethod="post"> to submit the form to a php
And I'm trying to retrieve the values in my php file with:
$link = $_POST['link'];
Why isn't this working? At first I thought it was because I had htmlspecialchars() but it's not coming through without it either. I can't find anything in any google search that even mentions anything related to this kind of problem (with a type="url" form)
What do I need to do to process form data with type of "url" in PHP with a $_POST?
Get your form method to be set to post e.g
<form method=post>,
if you submit the form and in the url in your browser u can see some more inf then be sure 2 check your form method
I think this is wrong,
method="post"
Its only method, not formmethod
Also make sure, you dont have one more for element name with link.

Filling out and submitting html forms with php without user input

I have the following form in a file called "foobar.html":
<!-- other stuff -->
<form method="post" action="foo.php?cat=1">
<input type="text" name="bar" />
<input type="submit" value="foobar" name="foobar" />
</form>
<!-- other stuff -->
And I open this file in a php script with fopen, how do I fill out and submit this form without any input from the user? Thanks
Parse out the action attribute with a HTML parser, and use curl to perform a POST to the appropriate target URL.
Read the entire fire into a variable. Rather than using fopen you might want to consider file_get_contents for that, it's a bit cleaner.
You'll then want to parse that string as HTML. You could use PHP's DOMDocument for that. Get the action and method of the form by traversing the DOM tree to the form tag and reading out those attributes. Next get the names of any inputs within the form tags. Use those names to generate a query string with your key=value pairs. If the method of the form is GET, then append that query string to the form action, otherwise save it in another variable.
Finally, use CURL to "submit" the form. That is, use the form action as the URL for a CURL request. If the form method was GET, you should have already appended the data to the URL, if the method was POST, you'll want to set the data for the CURL request to the data query string you generated from the form names.
If your question extend to how to know what data to fill into what form fields, that is pretty much impossible to solve. Certainly there are some input names you could look for and guess the required data but a universal solution is an impossible problem to solve.
Are you trying to have the user submit the form on their browser, without user interaction? If that's the case, you'll need to resort to javascript, something like:
<body onLoad="document.getElementById('autoSubmit').submit();">
<form id="autoSubmit">
(insert form here)
</form>
</body>
This will automatically submit the form. Some notes: not everyone has JavaScript enabled, so you might want to change the inputs to type="hidden", as well as add a nice big submit button that says Click Here.

Remember form value when return back to submit due to some error

After filling the form when submit, accidentally due to some filling error ,the form is not submit and return to back,in this condition the value of all text box is blank. i want to stable value of all fields in this condition . I'm using php with smarty framework. Please reply with solution as soon as possible.
Thanks.
If the form is submitted to the page that contains it then you will have access to the submitted values, and can use them to populate your form. For example, if you are submitting the form via POST:
<input name="something" value="<?=$_POST['something']?>" />
If you are submitting the form to a different script, you could send the values back to the page with the form as URL parameters, or you could use temporary session variables, and unset them when the input passes whatever validation you are using:
$_SESSION["temp_something"] = $_POST["something"]; //In form processing script
Then in your form:
<input name="something" value="<?=$_SESSION['temp_something']?>" /> <!--In form-->
You can fill the form fields, on the second round, by filling the content inside the value attributes of html tags, like so:
<input type="text" value="<?php echo $_REQUEST['test']; ?>" name="test">
Pay attention: this is a fast and simple solution. It gives you an idea. In good web programming practice you should sanitize the form data received by client in order to avoid security issues.

Is this a nice way to preserve a field value when an HTML form submitted?

I have a form with two submit buttons.
The user fills field-A and hits submit.
Done that, some input fields will be filled with data.
After that first submission, the value on the field-A should not disappear.
How can we preserve this value after the first submission?
Should we, on the field-A value attribute, place:
value="<?php echo isset($_POST['fieldA'])) ? $_POST['fieldA'] : ''; ?>" ?
The form submits to self.
Update - Additional details:
This is a form that will have two submit buttons on the same page (sort of speak).
Submit Button A - Will grab some data based on a input field, and fill the other input fields on that form.
Submit Button B - Once the form is filled, it will use all that data to do another submission.
This is a very simple case, no frameworks are in place here. I do have, however, some sort of MVP structure here.
Thanks in advance,
MEM
In general, such things being done using 2 forms, no one.
And GET method, not POST. At least for the first form.
But as you cannot ask a question, it's impossible to give you an answer.
Here you go:
index.php
<form action=edit.php>Enter name: <input name="name"><input type=submit></form>
edit.php
<? $row = dbget("row","SELECT * FROM domains WHERE name = %s",$_GET['name']); ?>
<form method="POST" action="save.php">
Edit data for <?=htmlspecialchars($row['name'])?>:</br>
NS: <input name="ns" value="<?=htmlspecialchars($row['ns'])?>"><br>
Another: <input name="another" value="<?=htmlspecialchars($row['another'])?>"><br>
<input type="hidden" name="name" value="<?=htmlspecialchars($row['name'])?>"><br>
<input type=submit>
</form>
save.php
do whatever you do usually to save info
I would store these values into $_SESSION, as user fabrik said. This way they can be stored across the entire form submission process(assuming it is multiple pages) and posted all at once at the end.
Assuming you're having some kind of submission system with a "next" button to go to the next set of forms, using session_start() and $_SESSION is certainly the best method. More information could be found here, or various tutorial sites--
http://php.net/manual/en/reserved.variables.session.php
It's ok to do that with $_POST, some people dont like the ternary operator but for me it works just fine. Although, there are better ways to deal with forms using O.O.P. You could create a class that manages your form, and pass an array to the constructor of that class (eventually you could pass the $_POST) and the class will create your form according to the info submited. You could even use the same class to valdidate your form
I don't see the need of using $_SESSIONS, cause this is not information that you need to preserve during the whole session.. or not?
Try this:
<?php
$fieldA = (isset($_POST['fieldA']) ? $_POST['fieldA'] : '')
?>
// and in your form
<INPUT type="text" name="fieldA" id="fieldA" value="<?=fieldA?>" />
as you mentioned, this should work.

Is it possible to send variables from HTML to another PHP file

I have a index.html where I would like to submit some coordinates that can be passed upon to separate PHP file; where it could perform a query. I am new to this.
HTML:
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
PHP query:
$query = "SELECT * FROM state WHERE LONG_HI<$_POST["Ymax"] AND LAT_HI<$_POST["Xmax"];
$result = mysql_query($query);
So is there a way to perform remote action from this HTML file to the specified PHP file?
Well, Forms can do the job. Is'nt it?
Yes
Either make an HTML form to accept the Xmax and Ymax parameters, and set the form action to the PHP file;
Or use AJAX to pass the data in the background and receive a response.
If both of these concepts are foreign to you, and you don't know JavaScript, get comfortable with the first option first.
Would you please describe in detail what you are about to do?
do you have a html form?
What kind of request do you do, clicking a link, sending the form?
The query does not contain any of the variables...
could you please post excerpts of the code? single lines are useless in most cases.
Regards,
Mario
use action attribute in FORM element to specify where the request will be sent to.
<form action="another.php" method="POST">
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
</form>
You just add few line with your code because to transfer any variable value from one form to another page we have to use 'form' method. So, we have to add form tag with your code. Transferring of data from one page to another page (any type of page like php, jsp, aspx etc) is done by two methods mainly - one of them is Post and another one is Get.
Difference between both the method is quite simple. In Post method, data from one page to another page travels in hidden form whereas Get is basically used to transfer value by displaying it at url. Post method example: user-name and password, and Get Method: any query fired at Search Engine.
<form name="form" action="filename.php" method="POST" >
//Your Code
</form>

Categories