Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with this PHP/JavaScript form validation?
    primarykey
    data
    text
    <p>I’m not sure whether the problem I’m having is with JavaScript or with PHP. </p> <p>My objective: To validate a simple yes no form using JavaScript then process it via PHP and have a message displayed.</p> <p>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.</p> <p>What am I doing wrong?</p> <p>Please note that this is unfinished code-I haven't added things like cookies, redirection etc. yet.</p> <p>Also I have a question about choosing answers. May I choose more than one reply as an answer?</p> <pre><code>&lt;?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 &gt;&gt;&gt;&gt; header("Location: http://www.mywebsite.com/".$dest); &lt;&lt;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); } } } ?&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;User Agreement&lt;/TITLE&gt; &lt;script language="javascript"&gt; function valbutton(thisform) { // validate myradiobuttons myOption = -1; for (i=thisform.myradiobutton.length-1; i &gt; -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 } &lt;/script&gt; &lt;/HEAD&gt; &lt;BODY&gt; &lt;H1&gt; User Agreement &lt;/H1&gt; &lt;P&gt;Before downloading you must agree to be bound by the following terms and conditions;&lt;/P&gt; &lt;form name="myform" METHOD ="POST" ACTION ="user_agreement4.php"&gt; &lt;input type="radio" value="NO" name="myradiobutton" /&gt;NO&lt;br /&gt; &lt;input type="radio" value="YES" name="myradiobutton" /&gt;YES&lt;br /&gt; &lt;input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="ANSWER" /&gt; &lt;/form&gt; &lt;/BODY&gt; &lt;/HTML&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload