Page 1 of 1

I can't get text values to show

Posted: Tue Sep 14, 2010 2:28 pm
by dj262501
Hello all,

I'm a novice php coder who is having some code trouble. Here's what I have:

Code: Select all

<?php
include "include/dbc.php";

include "include/header.inc";
?>

	<script type="text/javascript">
	<!--Hide Code
	// Function Statements

	//Function to confirm submit

function confirmSubmit() {
	var submitform = window.confirm("Are you sure you want to submit the activities?");
	if (submitform == true)
		return validateForm();
	return false;
}
// End Hide-->
</script>

<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="date" style="font-size:2em;"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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"/>&nbsp;&nbsp;<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> &nbsp;&nbsp;<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[Name].'"></div></td>';
[b]echo '<td><div><input type="text" name="duration" value="05:00"></div></td>';
echo '<td><div><input type="text" name="distance" value="5"></div></td>';[/b]
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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The bold lines are the ones giving me trouble. There is a table that is pulling info from a database. There are 2 fields (duration and distance) that require user info. The info is supposed to be passed to s 2nd page (add_activity.php). However, when data is entered, nothing is returned. Here,s what I get:

Code: Select all

Array
(
    [exerciseid] => Sweeping garage, sidewalk
    [duration] => 
    [distance] => 
    [submit] => Add Activities
)
It seems the only way for me to get the values of the fields named duration and distance are to hard-code them. This, of course, is not ideal. So my question is: what can I do to get these values to pass? Do I need to change something? Any information is helpful!

Re: I can't get text values to show

Posted: Tue Sep 14, 2010 6:03 pm
by social_experiment
dj262501 wrote:It seems the only way for me to get the values of the fields named duration and distance are to hard-code them
In the example script you gave, you have already hard-coded the values in? Just trying to clear up a bit of confusion because the output you show doesn't match the script (for me anyway).

If i understand correctly the information you don't want to hard-code is in the database, you just can't get them to display in the form? If yes, what are the names of the fields because you seem to have gotten it right with the value of 'exerciseid'. You apply the same principle to those fields as you did to the 'exerciseid' input tags.

Code: Select all

<?php echo '<input type="text" name="duration" value="'. $row['duration']. '" />'; ?>

Re: I can't get text values to show

Posted: Tue Sep 14, 2010 6:07 pm
by requinix
The way you've done it, there are multiple versions of all those fields. They all have the same name.
Only the last one on the page will be available to you, and apparently it's empty.

Use arrays. Name your fields with a [] at the end and you'll get an array. For example,

Code: Select all

echo '<tr><td><div>'.$row['Type'].'</div></td>';
echo '<td><div>'.$row['Name'].'</div></td>';
echo '<td><div><input type="text" name="duration['.$row['Name'].']"></div></td>';
echo '<td><div><input type="text" name="distance['.$row['Name'].']"></div></td>';
1. String always need quotes, even if they're array keys.
2. The hidden input doesn't serve any purpose so it was removed.
3. You can't put BBCode inside [syntax] tags ;)

Dump out $_POST and see what you get.

Re: I can't get text values to show

Posted: Wed Sep 15, 2010 6:38 am
by dj262501
social_experiment wrote:
dj262501 wrote:It seems the only way for me to get the values of the fields named duration and distance are to hard-code them
In the example script you gave, you have already hard-coded the values in? Just trying to clear up a bit of confusion because the output you show doesn't match the script (for me anyway).

If i understand correctly the information you don't want to hard-code is in the database, you just can't get them to display in the form? If yes, what are the names of the fields because you seem to have gotten it right with the value of 'exerciseid'. You apply the same principle to those fields as you did to the 'exerciseid' input tags.

Code: Select all

<?php echo '<input type="text" name="duration" value="'. $row['duration']. '" />'; ?>
I don't want to hard-code any of it. Duration and distance are textboxes in which the user must put information in. exerciseid is pulling information from a database, which is performing correctly. It's the two textboxes (duration and distance) that require user input that I can't seem to get it right without sticking a default value in them.

Re: I can't get text values to show

Posted: Wed Sep 15, 2010 6:46 am
by dj262501
tasairis wrote:The way you've done it, there are multiple versions of all those fields. They all have the same name.
Only the last one on the page will be available to you, and apparently it's empty.

Use arrays. Name your fields with a [] at the end and you'll get an array. For example,

Code: Select all

echo '<tr><td><div>'.$row['Type'].'</div></td>';
echo '<td><div>'.$row['Name'].'</div></td>';
echo '<td><div><input type="text" name="duration['.$row['Name'].']"></div></td>';
echo '<td><div><input type="text" name="distance['.$row['Name'].']"></div></td>';
1. String always need quotes, even if they're array keys.
2. The hidden input doesn't serve any purpose so it was removed.
3. You can't put BBCode inside [syntax] tags ;)

Dump out $_POST and see what you get.

I tried this, but it did not work. Maybe I did something wrong. It seems pretty logical though. I want to return the duration and distance of the $row['Name']. I don't know what else to do.

Re: I can't get text values to show

Posted: Wed Sep 15, 2010 11:48 am
by dj262501
I've been looking at this for awhile. Does anyone has any suggestions as to how I can fix this?

Re: I can't get text values to show

Posted: Thu Sep 16, 2010 1:51 am
by social_experiment
PM me the complete script as well as all fields in your database table please. Ill see if i can help you with it.