How to pick up form details using POST request from a PHP page which was accessed using GET request

佐手、 提交于 2019-12-25 02:43:33

问题


I have executed a php script through GET request ex:

http://localhost/example.php?id=9&name=exammple

Now the example.php has a form field

<form method="post" action="example.php">

---form fields used to post data on server---

</form>

Now on submitting the form, it throws up an error saying undefined indexes.

How do I submit a form in a page which was accessed using GET request? I am a PHP beginner.


回答1:


Use <form method="post" action=""> Then it will post the data to the same page (with the GET parameters) and you can get them using $_GET and the parameters from the form using $_POST or all with $_REQUEST.




回答2:


Add a

$_POST += $_GET;

before you access the $_POST variables or use anther request variable like $_REQUESTDocs if you want to allow others next to $_GET, too (namely $_COOKIES with PHP's default configuration).

A little explanation:

  • $_GET - contains all get variables.
  • $_POST - contains all post variables.

The

$_POST += $_GET;

will put all get variables into $_POST if they have not been send as post variable by creating a union of $_POST and $_GET.



来源:https://stackoverflow.com/questions/7736846/how-to-pick-up-form-details-using-post-request-from-a-php-page-which-was-accesse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!