Forum Moderators: coopster

Message Too Old, No Replies

requery mysql field

         

indiguy

3:43 am on Sep 24, 2009 (gmt 0)

10+ Year Member



Hi, without clicking a different button or manual page refresh, how would i get the new db values to be returned to the text box after inserted and confirmed the update?

ive tried header("Location: $_SERVER['PHP_SELF']... blah etc
but not working.
some code from form:

echo "<td><input type=\"submit\" value=\"Update\" name=\"button1_$id\" onclick='return confirm(\"Are you sure you want to update this?\")'></td>";

would i include something in the update if (isset[.. {.. area?
ive tried cut and pasting the main query, but nothing.

omoutop

2:14 pm on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



have you thought of using AJAX?
i cant think of anything else

rocknbil

7:04 pm on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You do this in the same process/script. A bit busy to type out all the logic, but something like

// See below for notes on this first line
if (isset($_GET['rec'])) { $current_id=$_GET[rec]; }

if (isset($current_id)) {
// open database
// get text for $current_id, store in $text
$recordfield = '<input type="hidden" name="current_id" value="' . $current_id . '">';
$action="update";
}
else {
$text='';
$action="add";
$recordfield = '';
}
$form = '
<form action="yourscript.php" method="post">
<input type="hidden" name="action" value="' . $action . '">' .
$recordfield .

<input type="text" name="txtval" id="txtval" value="' . $text . '">
</form>
';
echo $form;

So if action is add, you insert and get the record ID, store in $current_id. When it returns to the page, it goes into "edit" mode, will open the database record, and your script should do an update based on the action being "update."

One might think this is an easier solution,

<input type="text" name="txtval" id="txtval" value="' . $_POST['txtval'] . '">

But this is no indicator it properly inserted or updated. Additionally, if you are choosing this form out of an edit link,

<a href="yourscript.php?rec=1234">Edit 1234</a>

You will need to set and get the data for this record for editing, and there is no post data for the text field, it will be blank.

One other bit of advice,

<input type=\"submit\" value=\"Update\" name=\"button1_$id\" onclick='return confirm(\"Are you sure you want to update this?\")'>

What happens when the user presses the enter key? I'll tell you. It submits without the confirm. Put your handler on the form:

$form = '
<form action="yourscript.php" action="post" onSubmit="return confirm(\'Are you sure you want to update this?\')">
<input type="submit" value="Update" name="button1_' . $id . '">
</form>';

echo $form;