Non-static form action without javascript? - php

I have a <form> with an action attribute.
I would like to change the value of action based on the value of an <input>.
The value is provided by the user. If the value is page1.php, the form will be submitted to page1.php. If the value is page2.php, the form will be submitted to page2.php, and so on.
Right now I'm achieving this using JavaScript, however it doesn't work on a browser with JavaScript disabled.
Is there any way to make the action non-static without JavaScript?

You can do this without invoking any javascript at all.
PHP example;
<?php
/*if no destination was set previously the form will post back to itself*/
$action=isset($_Request['destination'])?$_Request['destination']:'';
?>
<form name='a-form-name' action="<?php echo $action;?>">
/*include other inputs etc as required.
Include a 'destination' input in _all_ forms involved.
You can of course name it whatever you like. But should always be the same name in all forms involved.
*/
<input type='hidden' name='destination' value='the-desired-destination-of-next-action'>
<input type='submit' name='whatever' value='Click here'>
</form>
When you press the submit button it will go to whatever destination you set in the previous submission or 'destination' in the url query string.
You can even make the destination hidden input's value dynamic by using a variable instead if need be. Branching can be quite easy and extensive using this method.
HTH

If you just want to receive the value on the server side, than using get as the form's method will work (you end up with page.php?my_var_name=my_var_value).
If, however, you want to direct the form to a completely different page based on the value of your input, you'll either have to use JS, or have a "catchall" page on the server side that gets the form and redirects to the final page based on the value.

Related

About PHP form , redirection

Suppose my Form codes look like this
URL : localhost/my-url.php
<form action="hello.php">
...bla bla bla
</form>
I will process the data in hello.php and i want to redirect to user to same url after processing (according to above example)
localhost/my-url.php
I know we can use header but i don't know how to get that url from which form was submited :(
Googled but didn't found any use full.
Thanks.
Add a hidden value in your form:
<input type="hidden" name="lastUrl" value="<?php echo $_SERVER['REQUEST_URI'] ?>" />
You now have the URL in $_POST['lastUrl'] data. You need to do it that complicated because $_SERVER["HTTP_REFERER"]; is send by the browser, and not all of them do this reliable.
You should put a hidden field in your form and set its value to current page url.
Then you submit the form and get the value of hidden field.
Then you can redirect user to hidden field (which is actually a URL of the page where you are submitting form) by using javascript or php.
You can use the
$_SERVER["HTTP_REFERER"];
to get the original URL where the form was posted from.
Remember to escape it, if you use it however. ]
Alternatively, you can process the form using AJAX, send process things (redirection) client-side.
Note that form data can be changed and intercepted if you wish to send the URL of the page as form data.

GET and POST is submitting on same time

I have html page and I have taken one form in it and other link outside the form .Form is Submitted by POST method,when I submitting form first time its ok and when I click link it pass data by GET method and when I again submit form then it send both GET and POST variable i.e form data and link data both.so what is the reason for that and how can I solve it.My html page is below
<html>
<body>
<form method='post'>
<input type=input name='name'/>
<input type=submit name='submit' value='submit'/>
</form>
<a href='check_global.php?page_number=6'>Page Number</a>
</body>
</html>
Because the form hasn't the action attribute, so it simply reload the page. When you submit it the first time it's all fine, but when you do it after clicking the link, the url is 'dirty' due to the data of the link, so you have both GET and POST values.
You can check wether the POST attribute is set ( if(isset($_POST['name'])) with php), in this case it has been submitted with the form
When you submit the form the second time you see the form parameters + the url parameter of the page (remember you clicked the link with the relative URL 'check_global.php?page_number=6').
To verify the above try this:
<?php
echo 'GET param ' . $_GET["page_number"];
echo 'POST param ' . $_POST["name"];
?>
As you can see you can access both types of parameters during a POST request.
Hope that helps.
Just to make the point, the OP did not indicate that the form was supposed to submit to anywhere but the current page. So just for funsies, here is the same basic idea, but with an action attribute value:
<form method="post" action="">
<input type="text" name="name"/>
<input type="submit" name="submit" value="submit"/>
</form>
Page Number
Notice that I've set it up so that, for whatever reason, the link points back to this same page and so does the form. The result:
First Load: form submit makes request with POST data to blah.php
Second Load: link follow makes request with GET (thanks to the query string) to blah.php?page_number=6
Third Load: form submit, using blank action to indicate that current page is where to post, makes request with POST form data to blah.php?page_number=6, thus having both POST form data and GET URL data.
So your options are to either set the action attribute value to blah.php so that it does not include the query string, or to accept that if you want to avoid the various ways of doing this in favor of having a more modular form (drop it in any page and you know it will post to that address), then to simply have the PHP backend check if $_POST['submit'] is set and if so, handle it like a form post and don't use any of the $_GET logic that might be screwing things up.
The link is never sending the form data as POST, and the POST data is not part of the GET array, so you know that when there is no POST, it's just get and if there is POST, it was a form submit, even if there is a GET array.
Or just use separate scripts so you don't get mixed up.

Hidden input (html form confirmation) - Server side script? (php)

I'm writing a site in PHP.
And I have a simple registration form.
<form>
<label...></label> <input .../>
</form>
I want to add a confirmation field:
<input type='hidden' name='hiddeninput' value="jn3kjnv3kjvn35">
But how does this code look on the server side?
Do I need to save every hidden value to the database whenever registration form is loaded?
I'm trying to make sure the form is not being filled in by bots.
That's why I need a random hidden value that is unique for every form submission.
Suppose every time I generate the registration page - I generate the unique value for "hidden" field.
When the user submits the form - how do I compare the submitted value to the one that was generated (as once it's generated - it's not stored anywhere in the site).
Basicly , you should use a function that generates a random string (hash for example)
and sessions to "remember" this string.
Aftet submitting the form , you'll check the INPUT's value and cross it with the SESSION's value.
For instance:
Form.php
<?php
$hash = md5(time());
$_SESSION['form_xx'] = $hash; //in case you have more than one form.
?>
<form method='post' action='do.php'>
..
..
<input type='hidden' name='secret_key' value='<?=$hash?>'>
</form>
do.php
if($_POST['secret_key'] == $_SESSION['form_xx']) //Just make sure your making the posted value SAFE
//He's ok.
else
die("arggg...those hackers");
You need to add a name to the field, too. It works like pretty much any other HTML field.
I now use Ruby on Rails framework that solves it out of the box.

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

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

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.

Categories