0

I have a button that generates after each table row is generated in the loop, the name of each consecutive button is generated by $a++ variable. How do i use the $_POST method on my edit_contact.php page so i can use the variable from the $_POST array?

The variable is stored in the $_POST array, i have checked with Print_r($_POST); for example when i click on the third row edit button of the table, it will display as:

Array ( [3] => edit ) 

Here is the code of the loop on my list_contact.php page:

$a = "0";

// print whether success or not

if ($rst)
{
    if (mysql_num_rows($rst)>0) // chech there are records
    {
        echo "<form name=addcontact method=post action=edit_contact.php>";
        echo "<table border=\"1\" cellspacing=\"0\">";

        /*** print out feild names ***/

        while ($row = mysql_fetch_array($rst)) // fetch each row
        {
            echo "<tr>";

            for ($i=0; $i<mysql_num_fields($rst); $i++) //for ech row print out field values
            {
                echo "<td>" . $row[$i] . "</td>";
            }

            echo "<td>" . "<input id=button type=submit name=" . $a++ . " value=Edit" . "</td>";

            echo "</tr>";

        }

    echo "</table>";
    echo "</form>";
    }

  else
    {
     echo "There are no records in the database";
    }
}

And here is the code i am having trouble with on my edit_contact.php:

$qry = "SELECT * FROM contacts WHERE ContactID = " . $_POST;

How can i get that post to reflect just my variable? ie 3

5
  • mysql_num_rows and other mysql functions are deprecated. Try using the mysqli extension or PHP Data Objects (PDO) Commented Dec 25, 2014 at 4:20
  • 2
    Since you're a student, this would be a good time to not learn to use the mysql_* functions since they are deprecated. Look into mysqli or PDO. Commented Dec 25, 2014 at 4:23
  • Unfortunately this is the material they have given us to learn. Though i will make note of it to my teacher, why am i paying to learn old material. Thanks Commented Dec 25, 2014 at 4:32
  • I hear that a lot from students. It's sad that the textbooks are a few years behind the time taught by teachers who are not practicing developers themselves and therefore don't keep up with the latest techniques Commented Dec 25, 2014 at 5:48
  • Yes you're right. Though i think in my case it's very concerning as i just looked up when this particular material was last reviewed, it was last reviewed 6th may 2014. This unit was also prepared by the teachers and delivered online, it's not even a textbook. So i think that it is completely unacceptable, i'll be putting in a formal complaint it think. Thanks for your help Commented Dec 25, 2014 at 8:21

3 Answers 3

1

You can do it once. You don't need any more fields. Just use foreach to access its key

foreach($_POST as $key => $value){
     if(strtolower(trim($value)) == "edit"){ // Validate if editing being sent
         // Display it if true
         echo $key; // and this is the variable you want
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Absolute champion! Exactly what i was looking for. Works perfectly
1

you could use a hidden input field so instead of:

echo "<td>" . "<input id=button type=submit name=" . $a++ . " value=Edit" . "</td>";

you could do:

echo '<td><form method="post"><input type="hidden" name="ContactID" value="'.($a++).'"><input id="button" type="submit" value="Edit"></form></td>';

then retrieve the hidden input field like:

$ContactID = $_POST['ContactID'];

Note: You will still need to properly escape your POST data before using it for SQL queries, but hopefully this will point you in the right direction.

4 Comments

I did try this, i added the hidden input field and it carries the variable into the $_POST array, though i can't retrieve it. Even with simple echo "Variable" . $_POST["ContactID"]; it says undefined index.
you can try adding echo '<pre>',print_r($_POST),'</pre>'; to see the POST data being sent. If "ContactID" isn't there, then there is an issue with the form.
I got it to work, though the ContactID variable when submitted to the $_POST array will always be the value of the last loop. So if there are ten contacts then the ContactID will always be 10 because each new hidden input generated in the loop has the same name and by the time the loop has finished ContactID is 10, where as the button names were an incremented variable so each new button was generated with a unique name, ie the table row number. Any other ideas?
This solution requires you to create a form for each row. You've probably forgotten to do that and set up only a single form
0

Restructure the code so that the name is actually a constant, and the value is dynamic.

echo "<td><input id=button type=submit name=\"edit\" value=\"" . $a++ . "\"></td>";

This way you can access it with:

$_POST['edit'];

And it will yield the value of: value=\"" . $a++ . "\" for the button that was clicked.

Which will return something like 3, as you desire.

1 Comment

Yeah, exactly, i need the button to say Edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.