Jump to content

I need help passing form variables to a second page.


dj262501

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.