开发者

What's wrong with this PHP/JavaScript form validation?

开发者 https://www.devze.com 2023-02-24 12:26 出处:网络
I’m not sure whether the problem I’m having is with JavaScript or with PHP. My objective: To validate a simple yes no form using JavaScript then process it via PHP and have a message displayed.

I’m not sure whether the problem I’m having is with JavaScript or with PHP.

My objective: To validate a simple yes no form using JavaScript then process it via PHP and have a message displayed.

My problem: When JavaScript is enabled and I click the radio button and submit it the PHP doesn’t output “YES status checked”. Instead it refreshes the page (ie. I think it simply posts the form to user_agreement4.php and does nothing else) When JavaScript is disabled and I click on the YES radio button and submit it, the message “YES status checked” displays correctly. Please note that the code below is for user_agreement4.php. The form will be submitted to itself.

What am I doing wrong?

Please note that this is unfinished code-I haven't added things like cookies, redirection etc. yet.

开发者_如何学C

Also I have a question about choosing answers. May I choose more than one reply as an answer?

<?php
// Set variables
$selected_radio = 'test';
session_start(); // start up your PHP session!

// The below code ensures that $dest should always have a value.
if(isset($_SESSION['dest'])){
    $dest = $_SESSION['dest'];
}

// Get the user's ultimate destination
if(isset($_GET['dest'])){
    $_SESSION['dest'] = $_GET['dest'];   // original code was $dest = $_GET['dest'];
    $dest = $_SESSION['dest'];          // new code
}
else {
echo "Nothing to see here Gringo."; //Notification that $dest was not set at this time (although it may retain it's previous set value)
}

// Show the terms and conditions page
//check for cookie

if(isset($_COOKIE['lastVisit'])){
        /*    
        Add redirect >>>> header("Location: http://www.mywebsite.com/".$dest);      <<This comment code will redirect page
         */
        echo "aloha amigo the cookie is seto!";
        }
else {
    echo "No cookies for you";
    }
//Checks to see if the form was sent

if (isset($_POST['submitit'])) {
//Checks that a radio button has been selected
    if (isset($_POST['myradiobutton'])) {
        $selected_radio = $_POST['myradiobutton'];
    //If No has been selected the user is redirected to the front page. Add code later
            if ($selected_radio == 'NO') {
                echo "NO status checked"; 
            }
    //If Yes has been selected a cookie is set and then the user is redirected to the downloads page. Add cookie code later
            else if ($selected_radio == 'YES') {
                echo "YES status checked";
                // header("Location: http://www.mywebsite.com/".$dest);
            }
    }
}

?>

<HTML>
  <HEAD>
    <TITLE>User Agreement</TITLE>
    <script language="javascript">
function valbutton(thisform) {
// validate myradiobuttons
myOption = -1;
for (i=thisform.myradiobutton.length-1; i > -1; i--) {
if (thisform.myradiobutton[i].checked) {
myOption = i;
}
}
if (myOption == -1) {
alert("You must choose either YES or NO");
return false;
}
if (myOption == 0) {
alert("You must agree to the agreement to download");
return false;
}
thisform.submit(); // this line submits the form after validation
}
</script>

  </HEAD>
  <BODY>
    <H1> User Agreement </H1>
    <P>Before downloading you must agree to be bound by the following terms and conditions;</P>
<form name="myform" METHOD ="POST" ACTION ="user_agreement4.php"> 
<input type="radio" value="NO"  name="myradiobutton" />NO<br />
<input type="radio" value="YES" name="myradiobutton" />YES<br />
<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="ANSWER" />
</form>




  </BODY>
</HTML>


See this line:

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

If the user presses the submitit button, and javascript is disabled, everything works as expected - the button inserts its name/value pair into the posted data right before the form gets posted, so $_POST['submitit'] is set.

If, however, javascript is enabled, the button doesn't trigger a postback itself, instead it calls a javascript function which posts the form. Unfortunately though, when you call form.submit(), it won't go looking for buttons and add their name/value pairs to the posted data (for various reasons). So you need to find a different way of telling whether you are processing a post-back; the easiest way is to just put a hidden field into your form and check for that, e.g.:

(in the HTML part, somewhere inside the <form></form>):

<input type="hidden" name="is_postback" value="1" />

...and then change your PHP check to:

if ($_POST['is_postback'] == '1')


Change your javascript to:

function valbutton(thisform) {
// validate myradiobuttons
myOption = -1;
for (i=thisform.myradiobutton.length-1; i > -1; i--) {
if (thisform.myradiobutton[i].checked) {
myOption = i;
}
}
if (myOption == -1) {
alert("You must choose either YES or NO");
return false;
}
if (myOption == 0) {
alert("You must agree to the agreement to download");
return false;
}
return true; // this line enables the form to submit as normal and is not actually required
}

And remove the "return false;" from the on click event of the button. Having the validation function return false on validation fail is sufficient to stop the from from validating.

This should enable your php to work as is.

0

精彩评论

暂无评论...
验证码 换一张
取 消