Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

forms - I can't use GET and POST at the same time in PHP

Near the top of my page, I have this:

<?php $id = $_GET['id']; ?>

Then I have some form check conditionals that read from POST:

if (isset($_POST['completeSubmit'])) {
        //code
}

And finally, I have an HTML form which looks like this:

<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"; ?>" name="complete" method="post">
<input type="submit" id="textButton" name="completeSubmit" value="[mark as complete]">
</form> 

The page is initially accessed by using GET with an id variable like this:

http://website.com/page.php?id=1

All subsequent form submissions (which get redirected to the same page) fail. I know you can't send both GET and POST in the same request, but seeing as my form is submitting to $_SERVER['PHP_SELF']."?id=$id" using POST shouldn't it work? This is my first time trying this so it is quite possible I've overlooked something trivial.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use get and post at the same time, but you shouldn't. If you want to continue to send the ID this is as simple as:

<form ...
   <input type="submit" ...
   <input type="hidden" name="id"
      value="<?php echo htmlspecialchars($_GET['id'], ENT_QUOTES); ?>" />
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...