0

I have a form on an HTML/PHP page.

I have the same exact code on other pages on the site, and it works fine. I cannot figure it out.

Form:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />

    <input name="ContestEntry" type="submit" class="submit" value="Enter me in the Raffle!" />

</form>

I looked at the HTML source code, and the action populates the correct page, and $prize_id populates the correct info in the value.

PHP:

if(isset($_POST['ContestEntry'])){

    //...code to enter data into form, irrelevent since it won't post anything anyway.

}

else {

    echo 'Nothing posted from form';

}

"Nothing posted from form" always shows, and no data is being entered into the database. I've tried changing the form name to 5 different things, thinking maybe there was a conflicting name somewhere, but nothing works.

Any ideas?

Erik
  • 227
  • 2
  • 8
  • 19
  • try dumping the contents of `$_POST` at the start of your form to see what you're getting from the client. Other than that, you're gonna need to show more code - there's nothing there for us to go on. – Kryten Jul 23 '14 at 21:29
  • I have it dumped, nothing shows: Array ( ). What other code can I show here, I don't know what else is relevant. – Erik Jul 23 '14 at 21:31
  • Double-check that the code you have above is really exactly what is in your PHP files; it should work, and it seems like you have a hidden typo somewhere. – elixenide Jul 23 '14 at 21:38
  • Copy/pasted it exactly, it's exactly what is on the site. I've looked over it 100 times to make sure there isn't a typo, it's baffling me, it should work. – Erik Jul 23 '14 at 21:40
  • use your browser debugger ( F12 ) and check the network tab then check the request and see what is being sent to the server. Just to see if it's client-side problem or server-side. – Ramy Nasr Jul 23 '14 at 21:47
  • remember if your PHP does any redirects, POST data is lost in the middle. – Ramy Nasr Jul 23 '14 at 21:49
  • Did you figure out what the problem was? – Ramy Nasr Jul 24 '14 at 14:47
  • I didn't. I rebuilt the page line for line (as a different file), uploaded it and it worked fine. Compared the two and they were exactly the same. The initial file still didn't work. Maybe it was corrupted on the upload, and got cached or something. No clue... – Erik Jul 24 '14 at 21:45

4 Answers4

1

The code you posted actually works for me.

But, if you still can't get it working, I would try checking to see if prize_id is set rather than the button. The PHP script is executed when the form is submitted.

Also, I would recommend that you don't use $_SERVER['PHP_SELF'] as the form action. According to https://stackoverflow.com/a/14093363/3593228, that can make it easy for attackers to insert malicious data. Instead, leave the action empty.

Community
  • 1
  • 1
Alex Yorke
  • 48
  • 1
  • 8
1

If all you need to achieve is check whether the form is submitted or not, it's better to check that the request type is a POST:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Process form
} else {
    // Nothing posted!

}

Also change your submit button to:

<button type="submit">Submit</button>

and see what happens

Ramy Nasr
  • 2,367
  • 20
  • 24
  • Thanks. In the end, more needs to be done than just checking if it's submitted, but I've stripped it down to the basics just to debug. – Erik Jul 23 '14 at 21:45
  • Still haven't. Going to strip the page of all code aside from the form and php for the form, hopefully something gives. – Erik Jul 23 '14 at 21:51
0

The if Condition is not working because you put it on Button, instead of this just make an hidden field in the form to check form is submit or not like as you already have one. Or make new for this form.

<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
Aadil Afzal
  • 63
  • 1
  • 11
  • I just tried adding the condition to the hidden field, it still didn't work, same thing... if(isset($_POST['prize_id'])){ } I've always had the condition on the button, since the button has a value, and it's always worked. – Erik Jul 23 '14 at 21:35
  • @AadilAfzal an input submit button name is passed when a form is posted. – Sean Jul 23 '14 at 21:38
0

Consider adding what you're looking for as another hidden field:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
    <input name="ContestEntry" type="hidden" value="Enter me in the Raffle!" />

    <input type="submit" class="submit" />

</form>
Wes King
  • 627
  • 4
  • 7