twilitegxa Posted September 23, 2008 Share Posted September 23, 2008 I have the code listed below, and I want to set it to where before submitting, the 3 entries must add up to 12, and if they do not, I want it to return a message to the viewer letting them know they need to recalculate and that the value must equal 12. How do I do this? <form action="insert#.php" method="post"> <table> <tr> <td><strong>Body:</strong></td> <td><SELECT NAME="body" SIZE="1"> <OPTION value="01">1</OPTION> <OPTION value="02">2</OPTION> <OPTION value="03">3</OPTION> <OPTION value="04">4</OPTION> <OPTION value="05">5</OPTION> <OPTION value="06">6</OPTION> <OPTION value="07">7</OPTION> <OPTION value="08">8</OPTION> <OPTION value="09">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td></tr> <tr> <td><strong>Mind:</strong></td> <td><SELECT NAME="birthdateday" SIZE="1"> <OPTION value="01">1</OPTION> <OPTION value="02">2</OPTION> <OPTION value="03">3</OPTION> <OPTION value="04">4</OPTION> <OPTION value="05">5</OPTION> <OPTION value="06">6</OPTION> <OPTION value="07">7</OPTION> <OPTION value="08">8</OPTION> <OPTION value="09">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td></tr> <tr> <td><strong>Soul:</strong></td> <td><SELECT NAME="birthdateday" SIZE="1"> <OPTION value="01">1</OPTION> <OPTION value="02">2</OPTION> <OPTION value="03">3</OPTION> <OPTION value="04">4</OPTION> <OPTION value="05">5</OPTION> <OPTION value="06">6</OPTION> <OPTION value="07">7</OPTION> <OPTION value="08">8</OPTION> <OPTION value="09">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td></tr> <tr><td><strong>Total:</strong></td> <td><input type="text" name="total" size="5" maxlength="2" /></td></tr> </table> <input type="submit" name="submit" value="Submit Stats" /> <input type="reset" name="reset" value="reset" /> </form> Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 23, 2008 Share Posted September 23, 2008 Before submision? With PHP? You could do that with AJAX, but that wouldn't make sense. If you want to do it before submission you will want to use JavaScript, but you will also need to do it server-side in PHP in case the user has JavaScript disabled. And in that case it is typically easiest if you have your form page submit to itself for processing. Here's the JS part <html> <head> <script type="text/javascript"> function setTotal(formObj) { var total = parseInt(formObj.body.value) + parseInt(formObj.mind.value) + parseInt(formObj.soul.value); formObj.total.value = total; formObj.submit.disabled = (total!=12); var error = false; if (total==12) { error = ''; } else if (total>12) { error = " (Over by +" + (total-12) + ")"; } else if (total<12) { error = " (Under by -" + (12-total) + ")"; } document.getElementById('errorSpan').innerHTML = error; return (error==''); } </script> </head> <body> <form action="insert#.php" method="post"> <table> <tr> <td><strong>Body:</strong></td> <td><SELECT NAME="body" SIZE="1" onchange="setTotal(this.form);"> <OPTION value="1">1</OPTION> <OPTION value="2">2</OPTION> <OPTION value="3">3</OPTION> <OPTION value="4">4</OPTION> <OPTION value="5">5</OPTION> <OPTION value="6">6</OPTION> <OPTION value="7">7</OPTION> <OPTION value="8">8</OPTION> <OPTION value="9">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td></tr> <tr> <td><strong>Mind:</strong></td> <td><SELECT NAME="mind" SIZE="1" onchange="setTotal(this.form);"> <OPTION value="1">1</OPTION> <OPTION value="2">2</OPTION> <OPTION value="3">3</OPTION> <OPTION value="4">4</OPTION> <OPTION value="5">5</OPTION> <OPTION value="6">6</OPTION> <OPTION value="7">7</OPTION> <OPTION value="8">8</OPTION> <OPTION value="9">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td></tr> <tr> <td><strong>Soul:</strong></td> <td><SELECT NAME="soul" SIZE="1" onchange="setTotal(this.form);"> <OPTION value="1">1</OPTION> <OPTION value="2">2</OPTION> <OPTION value="3">3</OPTION> <OPTION value="4">4</OPTION> <OPTION value="5">5</OPTION> <OPTION value="6">6</OPTION> <OPTION value="7">7</OPTION> <OPTION value="8">8</OPTION> <OPTION value="9">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td></tr> <tr><td><strong>Total:</strong></td> <td><input type="text" name="total" size="5" maxlength="2" /><span id="errorSpan"> (Under by -12)</span></td></tr> </table> <input type="submit" name="submit" value="Submit Stats" disabled="disabled" /> <input type="reset" name="reset" value="reset" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 23, 2008 Author Share Posted September 23, 2008 How do I submit the form to itself for processing? This form is going to be part of a multi-page spanning set of forms that the user fills out, and they submit after each page, but parts of it may need to be pulled from the previous page, such as $identity. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 23, 2008 Share Posted September 23, 2008 How do I submit the form to itself for processing? <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> Of course there will be more to it than that. I do not have enough knowledge of your overall process to give any specific advice. But, I typically have my forms POST to themselves so if there is an error in the submitted data I can redisplay the form to the user with the form filled with their previous data. If you have a multiple page form then I assume you are saving data in SESSION. So, have the page submit to itself. If validation fails, redisplay the form with the submitted data. If validation passes, save the data to SESSION and then do a redirect tot he next page. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 23, 2008 Author Share Posted September 23, 2008 I am going to begin working on making the pages link together, and apparently I will be using a session, which I was not sure how I'd do it before, but thank you for the advice. I will try your example once I get the pages linked together properly with the session. Thanks! Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 I am still working with this script, trying to understand how I can do this calculation with PHP. Can anyone help me out with some code for how to do this? I have some code for checking it in JavaScript, but I also need to check in PHP as well. Any help? Thanks! Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 The JavaScript code from a previous post doesn't appear to be working. Any suggestions on why? Or any other JavaScript suggestions? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 27, 2008 Share Posted September 27, 2008 You need to be more specific. When you say it "doesn't appear tobe working" - what does that mean. It is either owrking or not. If it is not working, in what way? Do you get error messages? Is it returning the wrong values? What? If I copy/paste the code I submitted above into a new htm page it works perfectly in IE and FF. SO I have no idea what you have done differently. As for similar code in PHP I don't know how to respond. Simply take the three POSTed values, add them together and test if they equal 12. Quote Link to comment Share on other sites More sharing options...
corbin Posted September 27, 2008 Share Posted September 27, 2008 http://www.tizag.com/phpT/postget.php http://www.w3schools.com/PHP/php_post.asp Read up on how to accept data. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2008 Author Share Posted September 27, 2008 Sorry, the JavaScript did work. I missed copying some things. Now checking out the links provided. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2008 Author Share Posted September 27, 2008 Okay, I've read up on the POST stuff. I kind of know how to actually submit my data in the table in my database, as I have a few pages that are able to do that now. What I need is the code to double check the value of the submitted data from the PHP side, just in case the user disables JavaScript or something like that. I know a little bit about validating using PHP, but I DON'T know how to validate with somethign like this. Like, I have registration page that checks the length of the username and password, and also checks to see if it's already been entered into the table. But, I don't know how to have PHP check the values and make sure they add up to 12 before submitting. Does anyone know how to do that? Quote Link to comment Share on other sites More sharing options...
corbin Posted September 27, 2008 Share Posted September 27, 2008 So you want to see: if(a+b+c = 12) Now, convert it to PHP with POST: if($_POST['var1'] + $_POST['var2'] + $_POST['var3'] == 12) (Chances are you would want to check if they were all numeric.) Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2008 Author Share Posted September 27, 2008 I have them set to come from a pulldown list, do I know they will be numeric, so I don't need to check for that, right? Do I put this PHP code on my pge that submits the data tot he table in the database or on this page with the form? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2008 Author Share Posted September 27, 2008 I have this code for the insert page: <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("smrpg", $con); $sql="INSERT INTO stats (body, mind, soul) VALUES ('$_POST[body]','$_POST[mind]','$_POST[soul]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Stats successfully added!"; mysql_close($con) ?> I'm assuming I will have to put it on this page, and probably say if it equal 12, to submit to table. Is that right? Where exactly would it go? Still a little unfamiliar with if/else statements and placement. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2008 Author Share Posted September 27, 2008 I have also updated my page. Here is the new code: <form action="insert4.php" method="post"> <table cellpadding=5px> <tr> <td><strong>Body:</strong></td> <td><SELECT NAME="body" SIZE="1" onchange="setTotal(this.form);"> <OPTION value="1">1</OPTION> <OPTION value="2">2</OPTION> <OPTION value="3">3</OPTION> <OPTION value="4">4</OPTION> <OPTION value="5">5</OPTION> <OPTION value="6">6</OPTION> <OPTION value="7">7</OPTION> <OPTION value="8">8</OPTION> <OPTION value="9">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td> <td><strong>Health Points:</strong></td><td><input type="text" name="health" size="3" maxlength="3" /></td></tr> <tr> <td><strong>Mind:</strong></td> <td><SELECT NAME="mind" SIZE="1" onchange="setTotal(this.form);"> <OPTION value="1">1</OPTION> <OPTION value="2">2</OPTION> <OPTION value="3">3</OPTION> <OPTION value="4">4</OPTION> <OPTION value="5">5</OPTION> <OPTION value="6">6</OPTION> <OPTION value="7">7</OPTION> <OPTION value="8">8</OPTION> <OPTION value="9">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td> <td><strong>Energy Points:</strong></td><td><input type="text" name="energy" size="3" maxlength="3" /></td></tr> <tr> <td><strong>Soul:</strong></td> <td><SELECT NAME="soul" SIZE="1" onchange="setTotal(this.form);"> <OPTION value="1">1</OPTION> <OPTION value="2">2</OPTION> <OPTION value="3">3</OPTION> <OPTION value="4">4</OPTION> <OPTION value="5">5</OPTION> <OPTION value="6">6</OPTION> <OPTION value="7">7</OPTION> <OPTION value="8">8</OPTION> <OPTION value="9">9</OPTION> <OPTION value="10">10</OPTION> <OPTION value="11">11</OPTION> <OPTION value="12">12</OPTION> </SELECT> </td> <td><strong>Attack Combat Value:</strong></td><td><input type="text" name="acv" size="3" maxlength="3" /></td></tr> <tr><td><strong>Total:</strong></td> <td><input type="text" name="total" size="2" maxlength="2" /><span id="errorSpan"> Total must equal 12. (Short by -12)</span></td> <td><strong>Defense Combat Value:</strong></td><td><input type="text" name="dcv" size="3" maxlength="3" /></td></tr> <tr><td colspan=3 align=right><strong>Total Character Points:</strong></td><td><input type="text" name="tcp" size="3" maxlength="3" /></td></tr> </table> <input type="submit" name="submit" value="Submit Stats" disabled="disabled" /> <input type="reset" name="reset" value="reset" /> </form> What I need now is to calculate the health, energy, attack combat value, and defense combat value as well as total character points based on values in the body, mind, and soul fields and have the text boxed disabled and updated accordingly. For each, this is what I need a claculation for: Health = [(Body+Soul)*5] Energy = [(Mind+Soul)*5] Attack Combat Value = [(Body+Mind+Soul)/3] (round down) Defense Combat Value = [(Body+Mind+Soul)/3] - 2 Total Character Points = needs to have a static value assigned to it (10) Can anyone help with this? These values will be submitted on the next page to a table in my database as well. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2008 Author Share Posted September 27, 2008 Please disreguard that last post. I do need these values calculated, but not until much later and other factors need to be calculated first. What I need right now is the PHP validation scrip that will check that the total of the stats is 12 before submitting the form. corbin suggested this code: if($_POST['var1'] + $_POST['var2'] + $_POST['var3'] == 12) Does anyone know where I am supposed to put this code? Does it need to go on this page to validate before submitting, or the next page, that actually doess the submitting? I am confused about this because I was suggested also to have this form submit to itself for validation first before sending to the next page, but if I do that, how do I set it up to have it send the information to the next page when the validation is successful so the values can be submitted? Quote Link to comment Share on other sites More sharing options...
corbin Posted September 27, 2008 Share Posted September 27, 2008 I have them set to come from a pulldown list, do I know they will be numeric, so I don't need to check for that, right? Do I put this PHP code on my pge that submits the data tot he table in the database or on this page with the form? Since when can people not modify drop downs? Anything client side = changeable. I hope you don't take this the wrong way, but errr.... Based on your questions so far, I think you need to further your grasp of basic concept of PHP. Perhaps learn more about processing user input. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2008 Author Share Posted September 27, 2008 Here's the problem. I do understand some about PHP, but I am still learning. Here is why I was confused. Maybe this will help you understand why I'm asking so many dumb questions: I have a page (register.php) that validates the username and password length and checks the database for the username, but it does this as well as the inserting query all on the same page. The problem I'm going to run into is that I am going to connect these pages with sessions, so this page will actually be posting to the next page, rather than insert4.php. I am just trying to get all the calculations and inserting done properly first, before setting the sessions and connecting them all. That way I can check for errors when inserting to make sure I have all the code right. I tried to just connect two pages before, and one page worked okay, but the second wouldn't, so I'm assuming that there was a problem with submitting, which is why I'm working on it first. And since I need these calculations done anyway, why not get them done now? I also didn't know how to do these calculations and validations with adding the form fields. I was also unaware (or maybe just didn't realize) that people could or would change the values in a dropdown box, so i guess as you suggested, I will need to verify that the values are numeric as well. This page (assignstats.php) is going to be the third page actually, and it will include the session variables from scout.php (or knight.php or villain.php) ...I have a first page that will check if the player wants to create a male or female and hero or villain, so that session will need to be carried over to the next page (either scout, knight, or villain.php) and that page will carry over to assignstats.php, and the next page will actually be either skattributes.php or ndattributes.php (depending on if they made a hero or villain). I know you probably don't need to know all these pages, but I was just trying to give you an idea of what I was trying to do. So basically, to wrap it all up, I need to make sure on this page that PHP validates that the form values equal 12 before going to the next page, whatever it may end up being. Is there a way to do this, now that you know my situation? I also was unaware (or possibly just didn't think) that people would or could change the values in a dropdown box, so as you suggested, I assume I will need to verify the fields are numeric as well. Quote Link to comment 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.