dj262501 Posted September 14, 2010 Share Posted September 14, 2010 Hello all, I'm new to php and I'm running into some trouble. I'm hoping someone will be able to help me. Here is the code I already have: <?php include "include/dbc.php"; include "include/header.inc"; ?> <style type="text/css"> .mydate{ color:#00F; text-decoration:underline; cursor:pointer; } </style> <script type="text/javascript"> function displayDate(d){ var date=new Date(); var D=date.getDate(); date.setDate(D+d); var YYYY=date.getFullYear(); var MM=date.getMonth()+1; MM<10?MM='0'+MM:null; var DD=date.getDate(); DD<10?DD='0'+DD:null; var span=document.getElementById('date'); span.innerHTML= 'Entries for '+MM+'/'+DD+'/'+YYYY; } onload=function(){displayDate(0)}; </script> <h1>Food Diary</h1> <div class="full"> <center><div><span class="mydate" onclick="displayDate(-1)"><img src="images/left_arrow.png" border="0">Yesterday</span> <span id="date" style="font-size:2em;"></span> <span class="mydate" onclick="displayDate(1)">Tomorrow<img src="images/right_arrow.png" border="0"></span></div><br /> <a href="#" onclick="displayDate(0);return false;">Today</a> </center> <div class="full"> <form name="exercise" id="exercise" method="GET" action=""> <center><table> <tr> <td><h3>Add an Activity</h3></td> </tr> <tr> <td><input name="NewSearchString" style="width: 100px" type="text"/> <input type="submit" value="Search" /> </td> </tr> <tr> <td> <select name="activity"> <option value="_">Activity Browse...</option> <option value="all">All Activities</option> <option value="biking">Biking</option> <option value="condition">Conditioning</option> <option value="dancing">Dancing</option> <option value="fish">Fishing & Hunting</option> <option value="Home">Home Activities</option> <option value="misc">Miscellaneous</option> <option value="music">Music Playing</option> <option value="occupation">Occupation</option> <option value="running">Running</option> <option value="sports">Sports</option> <option value="walking">Walking</option> <option value="water">Water Activities</option> <option value="winter">Winter Activities</option> </select> <input type="submit" value="Submit" /></td></tr></table></center></form> </td> </tr> </table> </center> <table width="100%"> <tr bgcolor="#66CC33"> <td><div>Activity</div></td> <td><div>Specific Activity</div></td> <td><div>Time (hh:mm)</div></td> <td><div>Distance</div></td> <td><div>Units</div></td> </tr> <tr bgcolor="#66CC33"> <td><div></div></td> <td><div></div></td> <td><div></div></td> <td><div class="Float"></div></td> <td class="cp_Distance"><div></div></td> </tr> <?php if(isset($_GET[activity])) { $category=$_GET[activity]; $result = mysql_query("SELECT * FROM exercise WHERE type='$category'"); ?> <form action="add_activity.php" method="post"> <?php while($row = mysql_fetch_array($result)) { echo '<tr><td><div>'.$row[Type].'</div></td>'; echo '<td><div>'.$row[Name].'<input type="hidden" name="exerciseid" value="'.$row[No].'"></div></td>'; echo '<td><div><input type="text" name="duration"></div></td>'; echo '<td><div><input type="text" name="distance"></div></td>'; echo '<td><div><select> <option value="mile">mile</option> <option value="Km">km</option> <option value="M">m</option> <option value="Yard">yrd</option> <option value="Feet">ft</option> </select></div></td></tr>'; } mysql_close(); ?> <tr><td colspan="6" align="center"><input type="submit" name="submit" value="Add Activities"></td></tr> </form> <?php } ?> <tr bgcolor="#66CC33"> <td><div></div></td> <td><div></div></td> <td><div></div></td> <td><div class="Float"></div></td> <td class="cp_Distance"><div></div></td> </tr></table> This code is supposed to pull info from a database and place it in a table. Once the user clicks the add activities button at the bottom, there values are supposed to be passed and displayed in the second page. Here's what I have for the second page: <?php include "include/dbc.php"; include "include/header.inc"; $activity = $_POST['activity']; $exercise = $_POST['exercise']; $duration = $_POST['duration']; $distance = $_POST['distance']; echo $exercise; echo $duration; echo $distance; ?> (Keep in mind that I am using this code to test the functionality of the page before I make it look nice) When I pass the values to the 2nd page, I get a black screen. Nothing is passing and I don't know why. Am I going about this the wrong way? Is there something I need to do to make the code better? Any suggestions are appreciated. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/ Share on other sites More sharing options...
the182guy Posted September 14, 2010 Share Posted September 14, 2010 Here's the problem: <form name="exercise" id="exercise" method="GET" action=""> The form method is set to GET which means all of the data will be sent in the query string not post. Change it to method="post" or change the php code to use $_GET instead of $_POST to pickup the variables. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110960 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 I've tried both of those changes and it's still not working. I have to leave the method as GET in order to pull the info from the database. I tried changing $_POST to $_GET on the second page, but I got the same result. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110962 Share on other sites More sharing options...
rwwd Posted September 14, 2010 Share Posted September 14, 2010 Hi there, Right, if you are having this all done off this one file all you need to do is just have the form action set as the same as the file name; then to catch the data being posted, start with this and carry on from there:- <?php //catch the data being posted & set error reporting error_reporting(E_ALL); if(isset($_POST['submit']) && !empty($_POST['submit'])){ //use print_r here to see whats being passed and if it is all the correct info //if it is, carry on with the processing } else{ header("location: the_name_of_the_file_here"); exit; } ?> HTML goes here, think of it as the 'flow' of the document... From that, you should be able to sort everything out... Cheers, Rw Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110969 Share on other sites More sharing options...
PFMaBiSmAd Posted September 14, 2010 Share Posted September 14, 2010 It's the second form (that gets produced when the first form is submitted) that is the issue - <form action="add_activity.php" method="post"> Aside from the fact that the hidden field name is name="exerciseid", and not "exercise", you should get what ever values you enter into the duration and distance fields. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110971 Share on other sites More sharing options...
merylvingien Posted September 14, 2010 Share Posted September 14, 2010 For what its worth I always quote my echo statements. echo "$exercise"; echo "$duration"; echo "$distance"; Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110972 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 Hi there, Right, if you are having this all done off this one file all you need to do is just have the form action set as the same as the file name; then to catch the data being posted, start with this and carry on from there:- <?php //catch the data being posted & set error reporting error_reporting(E_ALL); if(isset($_POST['submit']) && !empty($_POST['submit'])){ //use print_r here to see whats being passed and if it is all the correct info //if it is, carry on with the processing } else{ header("location: the_name_of_the_file_here"); exit; } ?> HTML goes here, think of it as the 'flow' of the document... From that, you should be able to sort everything out... Cheers, Rw I'm a little lost as to what you're asking me to do. I've never used print_r. I read about it on php.net but I'm still confused about how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110976 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 It's the second form (that gets produced when the first form is submitted) that is the issue - <form action="add_activity.php" method="post"> Aside from the fact that the hidden field name is name="exerciseid", and not "exercise", you should get what ever values you enter into the duration and distance fields. When I change it to exerciseid I get the number of the exercise based on the way it is stored in the database. The exerciseid is actually the only thing that gets returned. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110978 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 For what its worth I always quote my echo statements. echo "$exercise"; echo "$duration"; echo "$distance"; I've tried this as well with no luck. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110979 Share on other sites More sharing options...
PFMaBiSmAd Posted September 14, 2010 Share Posted September 14, 2010 When you do a 'view source' in your browser of the second form, does it contain what you expect? That would be the first step, get that form to be produced correctly so that it will submit the correct information to the add_activity.php page. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110980 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2010 Share Posted September 14, 2010 In the add_activity.php script, add this to the top of the script, then paste it's output here. That will show you exactly what is in the POST array to help in debugging this. echo '<pre>'; print_r($_POST); echo '</pre>; Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110982 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 In the add_activity.php script, add this to the top of the script, then paste it's output here. That will show you exactly what is in the POST array to help in debugging this. echo '<pre>'; print_r($_POST); echo '</pre>; Thanks for that debugging code. This is what I got: Array ( [exerciseid] => 94 [duration] => [distance] => [submit] => Add Activities ) Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110984 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2010 Share Posted September 14, 2010 OK, so that means the duration and distance fields are not set or have no value entered before the form was submitted to this page. I can also see that your <select> field that should contain the distance unit (mile, km, etc.) has no name attribute and therefore isn't being passed at all. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110990 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 OK, so that means the duration and distance fields are not set or have no value entered before the form was submitted to this page. I can also see that your <select> field that should contain the distance unit (mile, km, etc.) has no name attribute and therefore isn't being passed at all. I haven't set the value of the distance and duration fields. I thought it would be possible to post the field values based on the name. I haven't set the distance unit because I want to get this right first. Is there a better way to approach this? Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110993 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2010 Share Posted September 14, 2010 Well, if the fields have no value before the form is submitted, they'll have no value after it's submitted also, unless you code in a default value to be used in the event the field has no value or is empty. The good news is it appears to be working as it's currently written. Where are you having problems with it now? Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1110998 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 Well, if the fields have no value before the form is submitted, they'll have no value after it's submitted also, unless you code in a default value to be used in the event the field has no value or is empty. The good news is it appears to be working as it's currently written. Where are you having problems with it now? I want to pass the values the user puts in duration and distance. Should I set default values? Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1111019 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2010 Share Posted September 14, 2010 No, not necessarily. If those are going to be required input in the form, then you'd just need to validate the form in a way that would require valid values be entered before allowing the user to proceed. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1111023 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 No, not necessarily. If those are going to be required input in the form, then you'd just need to validate the form in a way that would require valid values be entered before allowing the user to proceed. I know I will have to validate the form, but is there anyway to pass the values the way the code is right now just for testing purposes? Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1111026 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2010 Share Posted September 14, 2010 Sure, you can just enter something in each text box before submitting the form. That will at least verify you have the syntax correct. Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1111027 Share on other sites More sharing options...
dj262501 Posted September 14, 2010 Author Share Posted September 14, 2010 Sure, you can just enter something in each text box before submitting the form. That will at least verify you have the syntax correct. I'm glad that part works. I guess now it's a question of getting the input values to show up on the next page. Is that where the form validation will come in? Quote Link to comment https://forums.phpfreaks.com/topic/213387-i-need-help-passing-form-variables-to-a-second-page/#findComment-1111083 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.