How do I post a new value with a form to PHP? I have a table of data and when I click one of the <td>-cells it switches to a text input area where I can type the new value and then update that row. Everything works just fine except the new value is not forwarded with that form, only the old one. I'm using mysql and php.

Here are parts of my code:

var edit = false;

function editme (e,name,variable) {
    if(!edit) {
        e.innerHTML = "<input type='text' id='" + name + "' cols='2' name='" + name + "' value='" + variable + "'>";
        edit = true;


 for ($i = 0; $i < mysql_num_rows($taulukko); $i++) {

$id = mysql_result($taulukko, $i, 0);
$pvm = mysql_result($taulukko, $i, 1);
$tankattu = mysql_result($taulukko, $i, 2);
$ajettu = mysql_result($taulukko, $i, 3);
$hinta = mysql_result($taulukko, $i, 4);
$kustannus = mysql_result($taulukko, $i, 5);

echo "<form id=\"Update\" action=\"bensa.php\" method=\"POST\">
<input type=\"hidden\" name=\"Update\" id=\"Update\" value=\"Update\">
<input type=\"hidden\" name=\"id\" id=\"id\" value=\"$id\">
<input type=\"hidden\" name=\"pvm\" id=\"pvm\" value=\"$pvm\">
<input type=\"hidden\" name=\"tankattu\" id=\"tankattu\" value=\"$tankattu\">
<input type=\"hidden\" name=\"ajettu\" id=\"ajettu\" value=\"$ajettu\">
<input type=\"hidden\" name=\"hinta\" id=\"hinta\" value=\"$hinta\">
<input type=\"hidden\" name=\"kustannus\" id=\"kustannus\" value=\"$kustannus\">
<td onclick=\"editme(this,pvm,$pvm)\">$pvm</td>
<td onclick=\"editme(this,tankattu,$tankattu)\">$tankattu l</td>
<td onclick=\"editme(this,ajettu,$ajettu)\">$ajettu km</td>
<td onclick=\"editme(this,hinta,$hinta)\">$hinta €</td>
<td onclick=\"editme(this,kustannus,$kustannus)\">$kustannus</td>
<td><input type=\"submit\" value=\"Update\"></p></td>
</form>"; }

So this works pretty find, except MySQL only receives the old value, because I set it in the hidden value. So how do I change that to accept the new value from editme() ?
Or is this possible/more easily done by some other way?

- AnttiK

Recommended Answers

All 2 Replies

So it's not possible to do at all? How much would I have to change the code to get it work? I have few tables/cell rows which need to be editable, but I don't want to use text areas on my main layout when viewing the page. Only when someone wants to edit the current values he/she get input areas.
New edit-page with text areas is not an option atm :(

- AnttiK

Your problem is that the editme function creates an input with the same name as an input that already exists (the hidden input). So you can only retrieve one value after you POST all the info. Easy fix is to change the name of the hidden input to something else like 'kustannus_old'. OR just delete all the hiddens cuz I dont' see how you are using them. Also, I'd add ' around the id when you call editme function - so you pass a string - it would look like this :
<td onclick=\"editme(this,'kustannus',$kustannus)\">$kustannus</td>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.