Post Var. Not Getting Passed To Switch Statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post Var. Not Getting Passed To Switch Statement

Post by Crashin »

I've got the following JS function to handle form cancellations:

Code: Select all

// function to cancel
function cancel_form(formName) {
  alert(formName.name);
  formName.submit();
}
And I've got the following cancel button on the form:

Code: Select all

<input type="button" name="submitIt" value="Cancel" onclick="return cancel_form(this.form)">
And here is my switch statement:

Code: Select all

<?php
// begin switch statment to handle POST request
switch ($_POST['submitIt']) {
  case 'Cancel': // if it is being cancelled
    echo "submitIt is CANCEL";
    break; // ---------- end of cancel case ----------
  
  case 'Submit': // if creating a new one
    echo "submitIt is SUBMIT";
    break; // ---------- end of create case ----------
  	
  default:
    echo "submitIt is NOTHING";
}
?>
However, I'm getting:
Notice: Undefined index: submitIt in c:\program files\apache group\apache\htdocs\projects\process_customer_suggestion.php on line 42
submitIt is NOTHING
upon submittal. What gives? I've read through php.net about switch statements, but nothing seems wrong there. :?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

I'm not sure if submit buttons pass their value that way. In your javascript do an alert(submitIt.value) and see what it shows. Read through this thread: viewtopic.php?t=19723
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Not sure but try adding a semicolon on your input statement....

Code: Select all

&lt;input type="button" name="submitIt" value="Cancel" onclick="return cancel_form(this.form);"&gt;
to check PHP value try ...

Code: Select all

if (isset($_POST['submitIt'])) {
  switch ($_POST['submitIt']) { 
    ....
  }
} else {
  echo(" !! Could not read value of submitIt ");
}
To confirm ... there doesn't seem to be anything wrong with the switch so it appears you are not passing the POST value, check javascript possibilities.
Post Reply