twilitegxa Posted September 18, 2008 Share Posted September 18, 2008 A couple questions about this. First question: How do I "connect" each page to each other so that all forms are submitted after the final form page? Second question: How do I write the code for the radio buttons on one of my pages to ensure the answer will be submitted into my database? Firstly, it's going to depend on what the person put for one field from the first page ($identity), and then the values of the radio buttons plus the value of $identity from the previous page will need to be submttted into the database for this particular page, along with the name of the field from the radio buttons. Example: <td width="34%">Agesim:</td> <td width="66%"><input name="bp" type="radio" value="1" />1 BP <input name="bp" type="radio" value="2" />2 BP</td> <td width="39%">Description: <input type="text" name="desc" /></td> In the example, what would need to be submitted for this particular answer would be: $identity (from the previous page) defect_id (which is Ageism) desc (if filled out, otherwise blank) bp (which is either 1 or 2, depending on what they chose) How do I name these appropriately so that each field is submitted into the database appropriately? Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/ Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 1) save their input in a hidden form value or a session while they work their way through the chain-forms. 2) it doesn't matter what you name them, as long as you know how to access them from the $_POST array (or sessions if you're using those to pass between forms). You act as the translator of submitted form values to how you insert them into your database, so you can name them whatever makes sense to you (and possibly to other programmers). Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-644356 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 1) I would suggest you post the first page to the second page, the second to the third, and so on. In each page, you would need to include hidden inputs that store the previous posts to be accumulated at the end. This might work for that: <?php foreach ($_POST as $k=>$val){ echo "<input type='hidden' name='$k' value='$val'>\n"; } ?> 2) You'll need to use the $_POST['bp'] variable after the form is submitted and use that data to insert into your DB. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-644358 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 1) I would suggest you post the first page to the second page, the second to the third, and so on. In each page, you would need to include hidden inputs that store the previous posts to be accumulated at the end. This might work for that: <?php foreach ($_POST as $k=>$val){ echo "<input type='hidden' name='$k' value='$val'>\n"; } ?> 2) You'll need to use the $_POST['bp'] variable after the form is submitted and use that data to insert into your DB. I'd advise escaping the input if you use the code there as it could potentially screw up your html (if a submitted value includes a ' for instance). Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-644362 Share on other sites More sharing options...
twilitegxa Posted September 18, 2008 Author Share Posted September 18, 2008 But what I'm wondering is how to make a hidden field I guess that will send the information. In my example from before, I have: <td width="34%">Agesim:</td> <td width="66%"><input name="bp" type="radio" value="1" />1 BP <input name="bp" type="radio" value="2" />2 BP</td> <td width="39%">Description: <input type="text" name="desc" /></td> How do I name the part that says Ageism for example? It is just a text on the page. Can I somehow set a hidden field to send to the database if a value is chosen for this option? I will have I think it's 24 different options like Ageism, and the viewer can choose the ones they want, and leave others blank. Next, would I be naming the bp radio buttons properly? The field in my database is named 'bp'. Have I coded it properly to where it would submit the value of either 1 or 2 into the bp field in my database? Same thing with my desc field. Is it named appropriately? The field is named 'desc' in the database for this one. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-644375 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 In your example, input "bp" is either 1 or 2, and on the next page, with that loop I listed inside of the form on your next page, it will create a hidden input named "bp" with a value of whatever the user selected. genericnumber1 is right, you would need to add a function like htmlentities() or something to check for special characters, I was just trying to keep it simple. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-644380 Share on other sites More sharing options...
twilitegxa Posted September 25, 2008 Author Share Posted September 25, 2008 Okay I was using the code: <?php foreach ($_POST as $key => $val) { echo '<input type="hidden" name="' . $key . '" value="' . htmlentities($val, ENT_QUOTES) . '" />' . "\r\n"; } ?> [/code At the bottom of one of my form pages, and it seemes to bring the data to the next page, but I can't get it to post to thr database. Here are the sample pages: page1.php [code] <html> <head> <title></title> </head> <body> <form action="page2.php" method="post"> <input type="text" name="firstname" /> <input type="text" name="lastname" /> <input type="submit" name="poneSubmit" value="Continue" /> </form> </body> </html> page2.php <html> <head> <title></title> </head> <body> <form action="script.php" method="post"> <input type="text" name="email" /> <input type="text" name="url" /> <input type="submit" name="Submit" value="Submit" /> <?php foreach ($_POST as $key => $val) { echo '<input type="hidden" name="' . $key . '" value="' . $val . '" />' . "\r\n"; } ?> </form> </body> </html> and script.php <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("smrpg", $con); $sql="INSERT INTO table VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[url]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Successful!";mysql_close($con) ?> What am I doing wrong? Why won't it post?[/code] Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650694 Share on other sites More sharing options...
twilitegxa Posted September 25, 2008 Author Share Posted September 25, 2008 I have been reading about sessions and it is apparently the better choice, but how do I get started using a session for this type of situation? I know I need to start the session on each page by using: <?php session_start(); // start the session ?> But what do I do next? And I use it on every single page, or just certain ones? Help me out guys! Please! Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650736 Share on other sites More sharing options...
Lee-Bartlett Posted September 25, 2008 Share Posted September 25, 2008 -- nvm -- Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650745 Share on other sites More sharing options...
twilitegxa Posted September 25, 2008 Author Share Posted September 25, 2008 Aww...got my hopes up! Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650754 Share on other sites More sharing options...
F1Fan Posted September 26, 2008 Share Posted September 26, 2008 A session could be a good answer. Here's a tutorial on sessions: http://www.w3schools.com/php/php_sessions.asp Basically, at the top of page 2, you'll want a session and you'll want to assign session variables the values of what was posted. Like this: $_SESSION['somefield'] = $_POST['somefield']; and so on with all of your post variables. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650956 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 Do I need to name the same variables in the session on page 3 or just name them once, and only name new ones from the previous page each new page? Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650967 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 And also, do I need to start a session on every page, including page 1 and the processing script page? Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650969 Share on other sites More sharing options...
genericnumber1 Posted September 26, 2008 Share Posted September 26, 2008 you need session_start() at the top of any page that you use the session functions or session superglobal. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650984 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 But I don't understand if that still means I need it on the first page and the page that submits into the database? Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650988 Share on other sites More sharing options...
genericnumber1 Posted September 26, 2008 Share Posted September 26, 2008 I haven't been keeping up with the thread, but if it the page uses any function that starts with "session_" or uses $_SESSION then you need session_start() at the top. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-650992 Share on other sites More sharing options...
F1Fan Posted September 26, 2008 Share Posted September 26, 2008 You won't be accessing or declaring any session variables on the first page, but you will need a session on all the other pages. Once you do a session_start() on a page and declare a $_SESSION variable, you can then access that saved value on any other page with session_start() on it, until session_destroy() is called, or until the session times out (set in the php.ini file on your server). I suggest you go through that tutorial I sent and try it yourself on a totally different (and new) page. Once you get the hang of it, modify your code here to work with it. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651005 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 The tutorial is so short, it doesn't really help much with what I need. It only shows how to start the session, then set the value (to a number, not a variable) and then to display it on the page. Then it shows how to check if the session has started, and if it has to add 1 to it, if not to start it. And then to destroy the session. I mean, it helps somewhat, with learnign about destroying the session at the end, but other than that, what you've said was more helpful than the tutorial. So what I've gathered so far is this: Each page after page one needs to have a session started on it, and on the page also needs to have this code on it: $_SESSION['somefield'] = $_POST['somefield']; filled in appropriately for the fields from the previous page, and on each page thereafter. Then, at the end, is the processing still the same as it was with one page and no session? Here is an example of my one page one: //connect to server and select database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); $db = mysql_select_db("smrpg", $conn) or die(mysql_error()); //create and issue query $sql = "insert into access_tracker values ('', '$page_title', '$user_agent', '$date_accessed')"; mysql_query($sql,$conn); ?> <title>Sailor Moon RPG - Character Creation - Successful</title> <meta HTTP-EQUIV="REFRESH" content="1; url=http://localhost/files/home.php"> </head> <div id="main"> <?php include("menu.php"); ?> <h1>Character Creation - Successful</h1> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("smrpg", $con);$sql="INSERT INTO scout (identity, charactername, elementofinfluence, age, birthdatemonth, birthdateday, birthdateyear, heightfeet, heightinches, bloodtype, hobbies, favoritecolor, favoritegemstone, favoritefood, leastfavoritefood, favoriteschoolsubject, leastfavoriteschoolsubject, strengths, weaknesses, goal, biography) VALUES ('$_POST[identity]','$_POST[charactername]','$_POST[elementofinfluence]','$_POST[age]','$_POST[birthdatemonth]','$_POST[birthdateday]','$_POST[birthdateyear]','$_POST[heightfeet]','$_POST[heightinches]','$_POST[bloodtype]','$_POST[hobbies]','$_POST[favoritecolor]','$_POST[favoritegemstone]','$_POST[favoritefood]','$_POST[leastfavoritefood]','$_POST[favoriteschoolsubject]','$_POST[leastfavoriteschoolsubject]','$_POST[strengths]','$_POST[weaknesses]','$_POST[goal]','$_POST[biography]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Character successfully created! Redirecting to Home page...";mysql_close($con) ?> Do I need a session started on this page as well, and then send the data to the database? Or is this code even going to work right with the session? I was just wondering if I would need to alter my code any. Please look it over ad let me know if I have anything wrong if what I've said I understood. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651025 Share on other sites More sharing options...
F1Fan Posted September 26, 2008 Share Posted September 26, 2008 OK, so here's the code you posted with the loop I suggested. With this short of a system, it would be the easier way to go: page1.php [code] <html> <head> <title></title> </head> <body> <form action="page2.php" method="post"> <input type="text" name="firstname" /> <input type="text" name="lastname" /> <input type="submit" name="poneSubmit" value="Continue" /> </form> </body> </html> page2.php <html> <head> <title></title> </head> <body> <form action="script.php" method="post"> <input type="hidden" name="firstname" value="<?php echo htmlentities($_POST['firstname'],ENT_QUOTES); ?>"> <input type="hidden" name="lastname" value="<?php echo htmlentities($_POST['lastname'],ENT_QUOTES); ?>"> <input type="text" name="email" /> <input type="text" name="url" /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> and script.php <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("smrpg", $con); $sql="INSERT INTO table VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[url]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Successful!";mysql_close($con) ?> What am I doing wrong? Why won't it post?[/code] If you notice, the only thing I added is in page2.php. The htmlentities function will change special characters into HTML entities (http://www.w3schools.com/php/func_string_htmlentities.asp). Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651034 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 But this example wasn't using sessions, was it? Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651040 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 And your code didn't work. I copied and pasted it exactly and I am still receiving this error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table VALUES ('liz','carter','twilitegxa','tripod')' at line 1 Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651041 Share on other sites More sharing options...
F1Fan Posted September 26, 2008 Share Posted September 26, 2008 No. A session would work here, and it would work better if you were connecting like four or five pages together, but if you're just going from page1.php, to page2.php, to script.php, then this example will be the quickest and most painless. The reason just copying and pasting what I did isn't working, is because your SQL is not right. You need brackets {} around any array inside double quotes, and you need quotes around your elements in arrays. $sql="INSERT INTO table VALUES ('{$_POST['firstname']}', '{$_POST['lastname']}', '{$_POST['email']}', '{$_POST['url']}')"; Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651043 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 I would rather understand the session thing, really. I mean I like the easier way and everything, but I can't seem to get it to work either. Plus, I will probably need to use sessions again, so I need to learn how to use them anyway. If I used the short examples from before with sessions, could you help me d those properly? I thought I was understanding some of it, except for the part when submitting to the database. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651045 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 Oh, okay. I will try that and see if it works. I need to understand hwo to use the sessions properly because I do have to do a longer span of pages (not sure how many yet, maybe 5-6) so I need to know how to use it for that. understading with a shorter set of pages might help so I can do the rest on my own. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651048 Share on other sites More sharing options...
twilitegxa Posted September 26, 2008 Author Share Posted September 26, 2008 Apparently that wasn't the problem because I changed that, and I'm still receiving this error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table VALUES ('liz', 'carter', 'twilitegxa', 'tripod')' at line 1 Same as before I think. Link to comment https://forums.phpfreaks.com/topic/124750-solved-multiple-page-form-submit-to-database/#findComment-651049 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.