Jump to content

Hidden html element defaulting to last value


OutOfInk

Recommended Posts

I have a while(); statement producing data from the mysql table.  But every time I post the data.. It is defaulting to the last value. I'm out of ideas on how to resolve this. When i Check the html source its displaying the data correct but once posted its going to the last value.

 

 

 

$start_date = date("Y/m/d", strtotime("- 7 days"));
$end_date = date("Y/m/d", strtotime("+ 7 days"));
 
$leader = mysql_query("SELECT information FROM `table` WHERE `Date` > '$start_date' AND `Date` < '$end_date'");
while ($redeal = mysql_fetch_row($leader)){
echo "<input type='hidden' name='round' value='$redeal[6]' />";
echo "<tr class='innertable'>";
echo "<td><input type='radio' name='beton' value='$redeal[0]' /></td>";
echo "<td align=left>$redeal[0]($<b>".number_format($redeal[3], 2)."</b>)</td>";
echo "<td>V</td>";
echo "<td><input type='radio' name='beton' value='$redeal[1]' /></td><td align=left>$redeal[1] ($<b>".number_format($redeal[4], 2)."</b>)</td>";
echo "<td align=left><input type='radio' name='beton' value='Draw' /> $51.00</td>";
echo "<td align=left>$redeal[2]</td>";
echo "<td align=left>$redeal[7]<br />$redeal[8]</td></tr><input type='hidden' name='pwnow' value='$redeal[5]' />"; <- 
}
Link to comment
Share on other sites

mysql_fetch_row fetches a single row.

use mysql_fetch_assoc instead of that.

 

Nonsense. They are both do the same, except mysql_fetch_assoc returns an associative array of field names, rather than a numerically index array

 

@OutOfInk

 

Your form code is correct. The problem is to with how you are naming your input fields. If your form contained one record it is fine, but for having multiple records it is not. This is because you have not named your input fields with a unique name, eg you cant have two fields named the same otherwise the latter field will always override the previous fields value. This is why only the last record is received when form is posted.

 

The solution to is to append square brackets to the field name, eg: 

$i = 0;
while ($redeal = mysql_fetch_row($leader)){
    echo "<input type='hidden' name='round[$i]' value='$redeal[6]' />";
    echo "<tr class='innertable'>";
    echo "<td><input type='radio' name='beton[$i]' value='$redeal[0]' /></td>";
    echo "<td align=left>$redeal[0]($<b>".number_format($redeal[3], 2)."</b>)</td>";
    echo "<td>V</td>";
    echo "<td><input type='radio' name='beton[$i]'' value='$redeal[1]' /></td><td align=left>$redeal[1] ($<b>".number_format($redeal[4], 2)."</b>)</td>";
    echo "<td align=left><input type='radio' name='beton[$i]' value='Draw' /> $51.00</td>";
    echo "<td align=left>$redeal[2]</td>";
    echo "<td align=left>$redeal[7]<br />$redeal[8]</td></tr><input type='hidden' name='pwnow[$i]' value='$redeal[5]' />";
    $i++;
}

You 'll see I added  [$i]  to the end of the field name, this is to ensure each field carries a unique name and more importantly each record has a unique index which we can use latter on in PHP to retrieve the values for that record. Example of this would be of using a loop like

foreach($_POST['round'] as $index => $value)
{
    $round = $value;
    $beton = $_POST['beton'][$index];
    $pwnow = $_POST['pwnow'][$index];

    echo "<b>Round:</b> $round - <b>Beton:</b> $beton - <b>Pwnow:</b> $pwnow<br />";
}
Edited by Ch0cu3r
Link to comment
Share on other sites

Thanks @Ch0cu3r 

 

This is the output i get;

 

Round: 1 - Beton: Draw - Pwnow: 1
Round: 1 - Beton: - Pwnow: 2
Round: 1 - Beton: - Pwnow: 3
Round: 1 - Beton: - Pwnow: 4
Round: 1 - Beton: - Pwnow: 5
Round: 1 - Beton: - Pwnow: 6
Round: 1 - Beton: - Pwnow: 7
Round: 1 - Beton: - Pwnow: 8
Round: 1 - Beton: - Pwnow: 9

 

That makes perfect sense, how would extract that first line as that is the line i need. Its radio option as they can only select on option out of the 9 rounds

Link to comment
Share on other sites

In that case it'll probably be best to revert back to your original code, but to output a new <form> for each record instead

while ($redeal = mysql_fetch_row($leader)){
    echo "<form action='submit-page.php' method='post' />"; // start a new form for each record
        echo "<input type='hidden' name='round' value='$redeal[6]' />";
        echo "<tr class='innertable'>";
        echo "<td><input type='radio' name='beton' value='$redeal[0]' /></td>";
        echo "<td align=left>$redeal[0]($<b>".number_format($redeal[3], 2)."</b>)</td>";
        echo "<td>V</td>";
        echo "<td><input type='radio' name='beton' value='$redeal[1]' /></td><td align=left>$redeal[1] ($<b>".number_format($redeal[4], 2)."</b>)</td>";
        echo "<td align=left><input type='radio' name='beton' value='Draw' /> $51.00</td>";
        echo "<td align=left>$redeal[2]</td>";
        echo "<td align=left>$redeal[7]<br />$redeal[8]</td></tr><input type='hidden' name='pwnow' value='$redeal[5]' />";
    echo '<input type="submit" name="submit" value="Submit" />'; // output submit button
    echo "</form>"; // close form
}
Edited by Ch0cu3r
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.