Jump to content

Problem with $_POST


tmyonline

Recommended Posts

Hi guys:  I can't figure this out.  Here's a snippet of my code:

 

...

while ($row = mysql_fetch_assoc($result)) {

    echo 'Sender: ' . $row['messSender'];

    $nameVar = $row['messTitle'] . $row['messTime'] . $row['contactID'];

    $nameVar = str_replace(" ", "_", $nameVar);

  ?>

    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">

  <?php

    echo '<input type="submit" name="' . $nameVar . '"' . ' value="Reply" />';

    echo '</form>';

    echo nl2br($row['messBody']);

}

 

  if (isset($_POST[$nameVar])) {

    echo 'nameVar = ' . $nameVar;

  }

 

Basically, I'm reading information out from MySQL.  Currently, my database table contains only two records:  X & Y.  The problem I have is that when I click on "Reply" for record X, somehow, $_POST[$nameVar] for record X is not defined.  When I click on "Reply" for record Y, $_POST[$nameVar] for record Y is defined and display the output as expected.

 

I have examined the source code and the "name" attributes contain different $nameVar for each record.  So, $_POST[$nameVar] is supposed to be defined regardless of which "Reply" button I click.  However, it only works for Y and not for X.  Any ideas ?  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/104582-problem-with-_post/
Share on other sites

Use a hidden value instead of the name of the submit button.

 

<?php
while ($row = mysql_fetch_assoc($result)) {
    echo 'Sender: ' . $row['messSender'];
    $nameVar = str_replace(' ','_',$row['messTitle'] . $row['messTime'] . $row['contactID']);
    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
    echo '<input type="hidden" name="nameVar" value="' . $nameVar . '" />';
    echo '<input type="submit" name="submit" value="Reply" />';
    echo '</form>';
    echo nl2br($row['messBody']);
}

  if (isset($_POST['submit'])) {
    echo 'nameVar = ' . $_POST['nameVar'];
  }
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/104582-problem-with-_post/#findComment-535317
Share on other sites

but this code:

 

echo '<input type="hidden" name="nameVar" value="' . $nameVar . '" />';

 

would create an extra input field which is not necessary.  The purpose of my form is to contain only the "Reply" button and nothing else.  Users are not to enter anything.  Basically, the code that I have is going to display all the email messages (read out from the database), each message is associated with a "Reply" button.  So, if he/she wants to reply to message X, he/she will click on the "Reply" button of that particular message.  He/she will not enter anything.  Therefore, I don't see how I need that line of code above!

Link to comment
https://forums.phpfreaks.com/topic/104582-problem-with-_post/#findComment-535354
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.