hari vallabh shukla
hari vallabh shukla

Reputation: 109

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>

Upvotes: 0

Views: 85

Answers (3)

Antonio Romano
Antonio Romano

Reputation: 285

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

Upvotes: 2

Anthony
Anthony

Reputation: 37065

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>

<a href="?page_number=6">Page Number</a>

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.

Upvotes: 0

javito
javito

Reputation: 1529

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.

Upvotes: 0

Related Questions