Link to home
Start Free TrialLog in
Avatar of gisvpn
gisvpnFlag for United States of America

asked on

Posting PHP Form with Hidden Value Issue

Hi - I have the following code which is used to post the value of a hidden form element to the page via PHP Post - the values and rows are generated from grabbing results from a database and it will output the results for each entry found.

The issue I have is the value posted called 'elm2' is always posted as the last one on the results set - i.e., it the last value on the table for t_ID is '33' then regardless of which row I click, it will always post as 33 - I think I can see the problem, all the outputted rows have the same name/id, but different values and I think it is just taking the last one on the list and posting this, not the one I am clicking.

Is this the problem and is there any way around it, or another way I could approach it?


<?php

$conn_task = new mysqli($dbhostname, $dbusername, $dbpassword, $dbdatabase);


$elm1 = $_POST['elm1'];
$elm2 = $_POST['elm2'];

echo "$elm1";
echo "$elm2";

?>

form start

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

    <?php

    if ($conn_task->connect_error) {
      die("Connection failed: " . $conn_task->connect_error);
    } 

    $sql = "SELECT * FROM xxxx";
    $result = $conn_task->query($sql);

    if ($result->num_rows > 0) {

      while($row = $result->fetch_assoc()) {

        $t_ID = $row["VWDF1"];
        $t_date = $row["tsk_aim_date"];

        echo "<tr><td>" . $row["t_ID"]. "</td><td>$t_date</td><td onclick='document.getElementById(\"PH33FORM\").submit()'><input type='hidden' value='$t_ID' id='elm2' name='elm2'>VALUE CELL - $t_ID</td></tr>";

      }
    } else {
      echo "0 results";
    }

    $conn_task->close();

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to name your input using the array format, so instead of calling it elm2, you need to call it elm2[]

<input type='hidden' value='$t_ID' name='elm2[]'>VALUE CELL - $t_ID</td>

Now when you post your form, $_POST['elm2'] will be an array that you'll need to loop over to get the values:

foreach ($_POST['elm2'] as $elm2) {
    echo $elm2;
}

IDs in a page also need to be unique, so don't add an ID to your element if it's used in a loop
Avatar of gisvpn

ASKER

Hi Chris - once I have the array as below, how do I match the right item in the array with the cell that had been clicked?
Expanding on Chris' solution
You give your names an index for instance elem2[123] where 123 is some unique identifier that identifies the row. In your case this would be $t_ID I imagine.

Now when the array arrives on the server the index of the item will tell you what row it came from.
As it stands, the value of each eml2 will be the value of the relevant $row["t_ID"]. So as you loop through them, the variable will hold the value:

foreach ($_POST['elm2'] as $elm2) {
    echo $elm2; // this will be the t_ID for each row
}

As your code stands, there's nothing else being submitted so the value in the other <td> elements won't get sent along with the form.
Avatar of gisvpn

ASKER

Hi both - thanks for the follow-ups. I should have added right at the top what I am doing with the code.

I have a table of records being listed. Each row on the table is uniquely identified by the $t_ID, the user can click on the row (or in this case the cell) to go in and effectively edit the record they clicked on - what I am trying to do here is identify the record they clicked on (and capture this in the 'elm2' variable) so I can pull the right record and present this to the user to edit (on the same page as an overlay).

Interestingly I can do what I am trying to do with a checkbox, where the user checks the checkbox and it posts the right t_ID to the page and I can pull the right record via SQL (as below) because it pulls the right t_ID value into the variable 'fmid1' for me, but not the same with the hidden HTML form element using a similar approach?

<input type='checkbox' value='$task_id' id='fmid1' name='fmid1' onchange='document.getElementById(\"PH33FORM\").submit()'>

Open in new window

OK. Without seeing your full code it's a little difficult to see what's going on, but you say:

the user can click on the row (or in this case the cell) to go in and effectively edit the record they clicked on

The code that you've posted looks like all it does when the user clicks the cell is to submit the whole form. Now if your form has say 20 records, then all 20 of those records would be submitted with the form. Unless I'm missing something, how would you know which cell was clicked on, even with a checkbox.
Ahh. I've just seen what you mean. Basically, what you have is an accidental solution.

A check box is only submitted to the form when it is actually ticked. If it's not ticked it doesn't get sent. Because you're effectively submitting the entire form, along with ALL records when a tickbox changes, although you submit all records, you only actually submit 1 checkbox, so you can use that to figure out what tickbox was ticked.

You say that you want to pull in the record to edit on an overlay. Are you making an AJAX call back to the server to do this ?

If all you need to do is send an ID back to the server, then there are better optiions to achieve this. Do you include jQuery in your page already ?
Avatar of gisvpn

ASKER

lol - good question on the checkbox! but it does work - let me see if I can pull a working abstract out and show you it end to end - it may have to be early next week, but will get it as soon as I can as i am curious about this one!
Avatar of gisvpn

ASKER

we posted almost at the same time :) - good to know about the checkbox, I guess that explains the accidental solution!

I am not using AJAX for the overlay but JavaScript and CSS - not using jQuery currently on the page
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gisvpn

ASKER

Nice! I managed to test this this afternoon and certainly works in terms of getting the right value passed to PHP.

I currently use the form to post other data with POST, but I think I can look to update these to use GET instead with some added precautions

Thanks again - your inputs are appreciated and I am learning something new each time!!