clay1 Posted February 19, 2010 Share Posted February 19, 2010 When this form is submitted, it is automatically resubmitted using JS. All my fields are carried over in the $_post except for total, and state which are radio buttons and a drop down. <p><input type="radio" name="Total" value="30" /> 1 Ticket - $30</p> <p><input type="radio" name="Total" value="50" /> 2 Tickets - $50</p></td> </tr> <tr><td><p><strong>First Name:</strong></p> <p><INPUT TYPE="text" NAME="FirstName" VALUE="<?=$_POST['FirstName']?>"></p></td> <td><p><strong>Last Name:</strong></p> <p><INPUT TYPE="text" NAME="LastName" VALUE="<?=$_POST['LastName']?>"></p></td></tr> Obviously I can't change the value of Total to $_POST['Total'] since that hasn't been set yet. What can I do here? Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/ Share on other sites More sharing options...
Catfish Posted February 19, 2010 Share Posted February 19, 2010 There is no instance of a "state" value in the sample code, so I won't comment about that. I put the following into a HTML file: <html> <form action="phpFreaksTest.php" method="POST"> <input type="radio" name="Total" value="30" /> 1 Ticket - $30<br/> <input type="radio" name="Total" value="50" /> 2 Tickets - $50<br/> <input type="submit" value="Submit"/> </form> </html> and then print_r($_POST) in the handling page and the total value passes over fine as expected. $_POST['Total'] becomes either 30 or 50 depending on user selection. If you are losing the value of $_POST['Total'] in your script, I am guessing it is being lost somewhere between user clicking submit and javascript submitting data to the server for PHP to handle. Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014684 Share on other sites More sharing options...
clay1 Posted February 19, 2010 Author Share Posted February 19, 2010 I guess I wasn't exactly clear. What I mean is this: 1. User fills out the form, selects 1 ticket and clicks submit 2. The page is redisplayed with all the user submitted text fields filled out.. ie first and last name. However the Tickets radio button is now no longer selected 3. The form is then sent to a 3rd party page where again the text fields are filled out with the user data, however the radio buttons and the drop down are not-- because unlike first and last name the value doesn't equal $_POST['whatever'] Does this make more sense? Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014687 Share on other sites More sharing options...
Catfish Posted February 19, 2010 Share Posted February 19, 2010 <input type="radio" name="Total" value="30" <?php if ($_POST['Total'] == '30') { echo 'checked'; } ?>/> 1 Ticket - $30<br/> <input type="radio" name="Total" value="50" <?php if ($_POST['Total'] == '50') { echo 'checked'; } ?>/> 2 Tickets - $50<br/> Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014691 Share on other sites More sharing options...
clay1 Posted February 19, 2010 Author Share Posted February 19, 2010 That didn't work. I was trying this but the post value is empty <?php if(!$_POST){ ?> <input type="radio" name="Total" value="30" /> 1 Ticket - $30</p> <p><input type="radio" name="Total" value="50" /> 2 Tickets - $50</p></td> <?php } else { echo "<input type=\"text\" name=\"Total\" value=\"". $_POST['Total'] . "\" >"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014694 Share on other sites More sharing options...
Catfish Posted February 19, 2010 Share Posted February 19, 2010 if the post value is empty then it is being lost somewhere along the line. you need ot debug and follow the values as they move around, identify where they are being lost and fix it. Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014696 Share on other sites More sharing options...
clay1 Posted February 19, 2010 Author Share Posted February 19, 2010 I can't see why.. the text fields are carrying over fine. Do you see any error in that code at all? Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014699 Share on other sites More sharing options...
Catfish Posted February 19, 2010 Share Posted February 19, 2010 this code? <?php if(!$_POST){ ?> <input type="radio" name="Total" value="30" /> 1 Ticket - $30</p> <p><input type="radio" name="Total" value="50" /> 2 Tickets - $50</p></td> <?php } else { echo "<input type=\"text\" name=\"Total\" value=\"". $_POST['Total'] . "\" >"; } ?> yes. <?php if(!$_POST){ ?> is saying "if the $_POST variable doesn't exist". You are actually interested in $_POST['Total'] and when you are submitting the form, some $_POST fields are coming over. therefore, the if statement will NEVER be true and executed unless no post data is submitted. it is only useful for an initial page load, but what about this circumstance: user loads initial page (radios are output) user selects form data but forgets to click a radio else { } is executed - there is no $_POST['Total'] data (even if you weren't losing the data like you are now) so your form is now rooted. you should change it to: if (!isset($_POST['Total'])) { output default radios } however, you can also use the code I pasted, which simply checks for a value of $_POST['Total'] and echos "checked" if it is either 30 or 50. data loss is not really going to show up in the code you have pasted because you said you are using JS to submit the form. My guess is you are losing the data in the JS. also, just because one or two or x amount of OTHER fields are coming over fine doesn't mean anything. it might be one little typo preventing the $_POST['Total'] value coming over. Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014703 Share on other sites More sharing options...
clay1 Posted February 19, 2010 Author Share Posted February 19, 2010 I don't believe the JS is doing anything at this point. This is just the user clicking submit This is the code above the form if(!$_POST['db']) { $action = 1; ?> <form name='form' method='POST' action='test3.php'> <? } else { $action = 2 ; include('includes/config.php'); ?> <FORM name='form' ACTION="removed" METHOD=POST> <? } ?> <table class="form1" width="100%" border="0"> <tr> <td colspan="3"><p><strong>Please enter your information. Name and address must match credit card billing address. All fields are required</strong></p> <p>How many tickets would you like to purchase? Bring a friend and save $10</p> <p><input type="radio" name="Total" value="30" /> 1 Ticket - $30</p> var_dump: array(24) { ["Total"]=> string(0) "" ["FirstName"]=> string(7) "edited" ["LastName"]=> string(6) "edited" Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014708 Share on other sites More sharing options...
clay1 Posted February 19, 2010 Author Share Posted February 19, 2010 Fixed by original coder Thanks for the help though Quote Link to comment https://forums.phpfreaks.com/topic/192597-javascript-form-submit-and-radio-buttons/#findComment-1014717 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.