This seems so simple but I can't remember how I've done it before.
Using PHP I'm posting a form from mysite.com/?x=y and want the resulting page to be mysite.com/?x=y&formx=formy...
Options I've tried don't quite give the desired result:
action - setting action="?x=y" clears the get variables if method="get" in place of those in the form. Prior knowledge of the get variables are also required.
method - although it seems logical to set method="get", this passes the form variables but clears any placed in action. Setting method="post" retains the current get variables but doesn't add the form variables/values.
Hidden field(s) - All get variables/values can be in hidden fields with method="get". This requires prior knowledge of the get variables and a lot of duplication if there are a lot of variables or forms. This so far is the closest solution.
Just set the form's "method" attribute to "get" instead of "post".
Example:
<form action="?x=y" method="get">
<input type="text" name="query" size="20">
<input type="submit" name="submit" value="Go">
</form>
I suppose you could :
either pass those variables as <input type="hidden" name="x" vaue="y" /> in your form.
or, maybe this might work : use "mysite.com/?x=y" as action for your form : with a bit of luck, those parameters will remain when the browser will post your form -- you should try, but it might work.
Of course, if you want those parameters to appear in the URL of the destination page, you'll have to use the GET method for your form.
Related
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.
This is a little bit quite confusing to explain. I want to know how to store a string into a variable and pass it to the next page and in that next page pass that variable again to another variable then that variable will pass it back to the original page.
Example:
I have two PHP pages. lets call them form1.php and form2.php
In form1.php, I will input Hello and it will be stored in var1.
var1 will be sent to form2.php and it is stored in var2 in form2.php as well.
in form2.php, var2 is passed to var3 and var3 will be sent back to form1.php.
And output Hello in form1.php using var3 not var1.
This is where I got stuck. Can't figure out how to do this. Please comment if the question is a little vague, even I cant seem to figure it out myself. Thanks.
You want to use Sessions. With sessions you can pass variables to a different page.
You have to start sessions on both pages.
session_start();
After that you can store variables in a session.
For example:
$var1 = $_SESSION['name'];
Now you can use IT in every page with session_start();
Well if you are actually using forms then you should be able to just grab the values of the variables in $_GET or $_POST (depending on the method used by the forms). You could just store the values in hidden inputs the user doesn't see and use that to pass things around:
<input type = "hidden" value = "<?php echo $_GET['var2'];?>" />
If this isn't what you need you may want to look at sessions to maintain state throughout your site.
My understanding is that you want to pass some data between two distinct forms - you need to consider that these forms are completely independent form each other so you will need to pass data back to the browser or leverage a server side session.
Depending on your use case you could:
Submit data to form1.php - this would then return a form with additional fields ready for submision to form2.php
The trick here - is that data that needs to be passed between forms would be included in the generation of the second form as hidden elements.
<input type="hidden" id="var1" value="data from form1" />
You can have as many hidden types as you need.
Be aware this approach is not very secure - so you may need consider defences e.g. csrf - or using sessions and tracking the data on the server side.
From what I understand, you need two forms to send data from the first to the second and from the second back to the first. In this communication, you don't need the third var, how about this?
form1.php
<form method=POST action="form2.php">
<input type="text" name="var1" value="<?=#$_POST["var2"];?>">
<input type="submit" value="Continue">
</form>
form2.php
<form method=POST action="form1.php">
<input type="text" name="var2" value="<?=#$_POST["var1"];?>">
<input type="submit" value="Continue">
</form>
When I use <form action="code.php?id=1" method="post"></form>, the form id is passed in the URL. But when I write the same code by replacing 'POST' by 'GET', the id is not passed to the URL.
Why?
When you submit a GET form, the values from the form are appended to the action URL, as the "query string" after the ?. Specifying an existing query string in the action attribute of such a form creates an ambiguity. Browsers don't merge the two query strings, they just throw away the old query string and build the new one based on the form.
With a POST form, there is no ambiguity: the data from the form is sent separately from the URL, so there is no need for the query string to be over-written.
However, it's probably best not to mix the two kind of parameters, so the solution is always to include your extra parameters as hidden fields, then it will work with both GET and POST forms:
<input type="hidden" name="id" value="1">
Better way is to pass id in hidden field.
<form action="code.php" method="post">
<input type="hidden" value="1" name="id" />
</form>
If your form is as below
<form action="code.php?id=1" method="post">
<input typ"text" name="username" />
<input type="submit" />
</form>
example script in code.php
<?php
print_r($_GET);
print_r($_POST);
print_r($_REQUEST);
?>
You will get form data in post array and url parameters in get array, in request you will get both get and post data in one array. But if you change from post to get method your form data added with the url instead of appending. This issue is because of ambiguity. To get solution i this situation, create a hidden field in your form those you also want to send with query string.
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
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>