Forum Moderators: coopster

Message Too Old, No Replies

PHP Losing POST value

         

skivingscholar

3:06 pm on Mar 30, 2010 (gmt 0)

10+ Year Member



Hi all, new here but this place is such an amazing resource.

Here's the scoop, and i'm sure it's simple. I press search with an "id", the data is looked up in the DB and sent to various input boxes and text areas, though when i try to update a record i lose the value of the id and can't find it anywhere! There's some debug code included there to let me see the variables.

<label for="keyword">Alert ID:</label>
<input id="id" name="id" class="text" type="text" style="border: 1px solid gray"/><br><br>
<center>
<input type="submit" class="formbutton" value="Search" name="search"/>
</center>
</div id>
<tr><td class="mainIn"><h1>results</h1><br>
<div id="code">

<?php

$conn = @mysql_connect( "localhost", "root", "root" ) or die( "Err:Conn" );
$rs = @mysql_select_db( "mydb", $conn ) or die( "Err:Db" );
if (isset($_POST['search'])) {

$var = @$_POST['id'];
$sql = "select * from app_mon where id='$var'";
$rs = mysql_query( $sql, $conn );

$vars = get_defined_vars();
print_r($vars);

while( $row = mysql_fetch_array( $rs ) ) {
echo( "<label for=\"id\">Alert ID:</label>" );
echo( "<input id=\"id\" name=\"id\" disabled value=\"{$row['id']}\" class=\"text\" type=\"text\" style=\"border: 1px solid gray\"/><br><br>");
echo( "<label for=\"mfrom\">From:</label>");
echo( "<input id=\"mfrom\" name=\"mfrom\" value=\"{$row['mfrom']}\" class=\"text\" type=\"text\" style=\"border: 1px solid gray\"/><br><br>");
echo( "<label for=\"mto\">To:</label>");
echo( "<input id=\"mto\" name=\"mto\" value=\"{$row['mto']}\" class=\"text\" type=\"text\" style=\"border: 1px solid gray\"/><br><br>" );
echo( "<label for=\"msent\">Sent:</label>");
echo( "<input id=\"msent\" name=\"msent\" value=\"{$row['msent']}\" class=\"text\" type=\"text\" style=\"border: 1px solid gray\"/><br><br>");
echo( "<label for=\"msubject\">Subject:</label>");
echo( "<input id=\"msubject\" name=\"msubject\" value=\"{$row['msubject']}\" class=\"text\" type=\"text\" style=\"border: 1px solid gray\"/><br><br>");
echo( "<label for=\"mbody\">Body:</label>");
echo( "<textarea style=\"height: 300px; border: 1px solid gray\" id=\"mbody\" name=\"mbody\">{$row['mbody']}</textarea><br><br>");
echo( "<label for=\"mresolution\">Service Desk Action:</label>");
echo( "<textarea style=\"height: 300px; border: 1px solid gray\" id=\"mresolution\" name=\"mresolution\">{$row['mresolution']}</textarea><br><br>");
echo( "<label for=\"mresolution2\">2ndline Support Action:</label>");
echo( "<textarea style=\"height: 300px; border: 1px solid gray\" id=\"mresolution2\" name=\"mresolution2\">{$row['mresolution2']}</textarea><br><br>");
echo( "<label for=\"Added By\">Added By:</label>");
echo( "<input id=\"maddedby\" name=\"maddedby\" value=\"{$row['maddedby']}\" class=\"text\" type=\"text\" style=\"border: 1px solid gray\"/><br><br>");
echo( "<label for=\"description\">Further Information:</label>");
echo( "<textarea style=\"height: 300px; border: 1px solid gray\" id=\"description\" name=\"description\">{$row['description']}</textarea><br><br>");
echo( "<center>");
echo( "<input type=\"submit\" class=\"formbutton\" value=\"Update\" name=\"update\"/><br><br>");
}
} else if (isset($_POST['update'])) {
echo( "<input id=\"id\" name=\"id\" value=\"{$row['id']}\" class=\"text\" type=\"hidden\" style=\"border: 1px solid gray\"/>");
echo( "<input id=\"mfrom\" name=\"mfrom\" value=\"{$row['mfrom']}\" class=\"text\" type=\"hidden\" style=\"border: 1px solid gray\"/>");
echo( "<input id=\"mto\" name=\"mto\" value=\"{$row['mto']}\" class=\"text\" type=\"hidden\" style=\"border: 1px solid gray\"/>" );
echo( "<input id=\"msent\" name=\"msent\" value=\"{$row['msent']}\" class=\"text\" type=\"hidden\" style=\"border: 1px solid gray\"/>");
echo( "<input id=\"msubject\" name=\"msubject\" value=\"{$row['msubject']}\" class=\"text\" type=\"hidden\" style=\"border: 1px solid gray\"/>");
echo( "<textarea style=\"height: 0px; border: 0px solid gray\" id=\"mbody\" name=\"mbody\">{$row['mbody']}</textarea>");
echo( "<textarea style=\"height: 0px; border: 0px solid gray\" id=\"mresolution\" name=\"mresolution\">{$row['mresolution']}</textarea>");
echo( "<textarea style=\"height: 0px; border: 0px solid gray\" id=\"mresolution2\" name=\"mresolution2\">{$row['mresolution2']}</textarea>");
echo( "<input id=\"maddedby\" name=\"maddedby\" value=\"{$row['maddedby']}\" class=\"text\" type=\"hidden\" style=\"border: 1px solid gray\"/>");
echo( "<textarea style=\"height: 0px; border: 0px solid gray\" id=\"description\" name=\"description\">{$row['description']}</textarea>");
echo( "<center>" );
mysql_query ( "UPDATE app_mon SET mfrom='{$_POST['mfrom']}' WHERE id='$'");

$vars = get_defined_vars();
print_r($vars);

echo "Record Updated!";

Matthew1980

3:33 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there skivingscholar,

Welcome to the forum ;-p

When checking the submission of forms, always check that the keys are as you want them by using print_r($_POST); on the recieving page, from that you can erradicate miss spellings etc.

Checking and handling data once submitted:-

if (isset($_POST['submit']) && ($_POST['submit'] == "search")){
//process data

Also, why are you supressing error's (@$_POST, @mysql...) I would have thought the more info you get the better you will be, good coding practise etc ;-p

And:-

$sql = "select * from app_mon where id='$var'";

Should be:-

$sql = "SELECT * FROM `app_mon` WHERE `id` = '".$var."'";

This will make the query function now ;-p

And for the echo's in the while(), why not break out of php into html, then back in for echoing the vars, as they might not be echoed as the format given there looks suspicious to me..

Sorry to pick fault ;-p

Hope this helps you a little ;-p

Also just noticed this too:-

mysql_query ( "UPDATE `app_mon` SET `mfrom` = '".$_POST['mfrom']."' WHERE `id` ='".$id."'");

Cheers,
MRb

skivingscholar

4:14 pm on Mar 30, 2010 (gmt 0)

10+ Year Member



Hey, thanks for the tips, you're right i should dip out of php and back in again instead of just echoing everything, same with the error supressing, I wasn't even aware that's what was happening

The actual search query works fine and the various fields are populated, it's when i come to hit update, the value of $var or $id just cant be found for some reason.

The update mysql at the bottom isn't complete as i just cant find any identifier in the post values to actually use as a reference to update the record.

Matthew1980

5:49 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there skivingscholar,

Check the vars, your checking for the wrong one ;-p

this piece of code needs changing:-

else if (isset($_POST['update'])){

try:-

else if (isset($_POST['submit']) && ($_POST['submit'] == "Update")) {

because you have it set as key 'submit' value => Update:-

echo( "<input type=\"submit\" class=\"formbutton\" value=\"Update\" name=\"update\"/><br><br>");

Check the print_r array, you will see what I mean. Because there are two distinct submit button's on the form, you have to separate them with the && part of the evaluation clause. Hope that makes sense.

As for the id not being there, I assume as you are checking the page source, if it's a blank, then the var isn't being echoed.

Anyway, good luck, and remember that array keys in all globals are case SeNsItIvE, unless you specify otherwise.

Cheers,
MRb

[edited by: Matthew1980 at 5:58 pm (utc) on Mar 30, 2010]

Readie

5:58 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think "Submit" is a reserved name, change it to something like "subbut".

Matthew1980

6:03 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Readie :)

Submit can be used many times, so long as the value key is different to differentiate it from the others, hence the:-

&& ($_POST['submit'] == "some_name")) in the submit catch if clause. It's Ok to check to see if the key isset() but you need to check it's value too ;-p

Either that or I have misunderstood you :)

Cheers,
MRb

Readie

6:43 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you mis-understood me. :)

I seem to recall reading it's a name reserved by PHP - as in, it's a bad idea to use it, like date, int, asc etc.

Could be wrong though.

Matthew1980

7:07 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Readie :)

You got me wondering:-

[php.net ]

I can't find it here.

I suppose that if it was, form submit buttons would be called something else, I can only see it defined twice as two different buttons, I can't see another instance of submit anywhere else, either that or I need new glasses ;-p

Cheers,
MRb

Readie

7:20 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ctrl/F doesn't turn it up either, so I must be mistaken.

Ignore me in this thread then :)

rocknbil

9:23 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I seem to recall reading it's a name reserved by PHP


It's not reserved in PHP, it's reserved by the DOM. This problem originates from the document, not in PHP. A short prequel,

<input type="submit" name="submit" value="Submit">

Naming or ID'ing an item submit, reset, - and here's a surprise one I spent hours figuring out - search (as in the original topic's example), can create some serious head-scratching problems as to why something is not working in Javascript, or why some variables that should be posting are not posting.

Second, submit and search are both functions in Javascript:

document.forms[0].submit()

So if you use [some-form].submit() somewhere in the scope of your current javascript, executing 'document.getElementById('submit')' will give you "object doesn't support this property or method." Because the submit() function doesn't have an id to get. :-)

Although you can sometimes get lucky and create conditions where you've id'ed or named something as "submit" and it works, you're just that, lucky, and its a very bad habit to get into.

You use document ID's to manipulate the document DOM/display via client side scripting and CSS. ID's are not posted on submit. Names are posted on submit.

Returning to the original problem:

I press search with an "id", the data is looked up in the DB and sent to various input boxes and text areas, though when i try to update a record i lose the value of the id and can't find it anywhere!


So something like

<input type="text" name="myid" id="mid">

$_POST['mid'] is of course not set. $_POST['myid'] should hold the value.

Readie

9:39 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not reserved in PHP, it's reserved by the DOM.

It was a half-remembered read-it-in-passing in my mind :)

Come to think of it now, I think it was in one of your posts I read it :/

Matthew1980

9:59 pm on Mar 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

So it is a reserved word but not in php, I never considered the possibility that it was reserved for the browser side of things, Well we live and learn. Thanks rocknbil.

Readie: It sounded plausible but I couldn't place it for php, that's why I queried it ;-p

Cheers,
MRb

skivingscholar

9:29 am on Mar 31, 2010 (gmt 0)

10+ Year Member



Hi chaps, many thanks for all your really useful input!

Unfortunately
else if (isset($_POST['submit']) && ($_POST['submit'] == "Update"))

Won't work, as presing the update button simply doesnt execute that line of code and the functionality isn't called.

In addition, when i rename <input id=\"id\" name=\"id"

to <input id=\"id\" name=\"myid" I still can't find the value.

As a quick fix, is there anyway just to ensure I post absolutely everything.

var_dump(get_defined_vars()); is actually showing:

["id"]=>
string(0) ""
["mfrom"]=>
string(17) "netasaa s/w owner"

How annoying!

skivingscholar

10:06 am on Mar 31, 2010 (gmt 0)

10+ Year Member



Hahahaha,

Worked it out.

disabled value

When you disable an input box to make the content un editable it doesn't seem to post the value. How wacky is that?

I'll just have to remove that edit box and have it post it as html instead sicne there's no clear work around here

skivingscholar

10:09 am on Mar 31, 2010 (gmt 0)

10+ Year Member



can use readonly instead, perfect.

Thanks guys

Matthew1980

12:18 pm on Mar 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there SkivingScholar,

That's quite right, does what it says on the tin. Read only will still be active during POST, but if you disable it, your turning it off.

Doh! I should have seen that. I guess tiredness was to blame ;-p

as for the:-

else if (isset($_POST['submit']) && ($_POST['submit'] == "Update"))

Couldn't you just place two if clauses then you wouldn't need to do the "if( a isn't true) elseif (try b instead) elseif (or C perhaps)

only difference would be is that you would probably need an exit; in the if to stop repeated content. Just a thought there.


As a quick fix, is there anyway just to ensure I post absolutely everything.


print_r($_REQUEST);

I think this should print everything that has been posted, including the $_POST array, place that at the top of the receiving file ;-p else just do: print_r($_POST); great for debugging.

Glad you fixed it anyway, have fun with the rest of the project anyway.

Cheers,
MRb