• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Show alert message after updating record

Participant ,
Apr 19, 2019 Apr 19, 2019

Copy link to clipboard

Copied

Hello All,

Please could someone kindly review my code and advice where I am wrong. Below is a code on how to show an alert message after updating a record  but it seems not to work. I use similar code for insert and its working well but on Update its not showing any message

Below is the UPDATE  code

<?php

if(isset($_POST['submit']))

{

$ass_id=$_POST['ass_id'];

$txpaid=$_POST['txpaid'];

$editedby=$_POST['editedby']; 

$dateedited=$_POST['dateedited'];

mysql_select_db ($database_DB,$DB);

$qur="UPDATE tbl_assess SET txpaid='$txpaid', editedby='$editedby', dateedited='$dateedited' WHERE ass_id=$ass_id";

$retval = mysql_query($qur,$CRIRS_DB) or die(mysql_error());

if ($retval == true) {

$result='<div class="alert alert-success">Your assessment was successfully posted</div>';

}else {

$result='<div class="alert alert-danger">Sorry there was an error posting your assessment. Please try again</div>';

}

header('Location: ' . $_SERVER['HTTP_REFERER']);

}

?>

Here is the code above my form that shows the message

<div class="col-sm-10 col-sm-offset-2">

            <?php echo $result; ?>   

</div>

Your review tips is highly appreciated...

Thank you in anticipation.

Mike

Views

4.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 23, 2019 Apr 23, 2019

Start a php SESSION by inserting session_start(); BEFORE anything else on your page:

<?php

session_start();

?>

Obviously you then have a connection to your database.

Below is the code to execute once the 'submit' button has been clicked: (include the correct variable id name for your $_POST - $***_id=$_POST['***_id']; and your sql query WHERE ***_id=$***_id in the code below)

<?php

if(isset($_POST['submit'])) {

$***_id=$_POST['***_id'];

$txpaid=$_POST['txpaid'];

$editedby=$_POST['editedby'];

$dateedited=$_

...

Votes

Translate

Translate
LEGEND ,
Apr 19, 2019 Apr 19, 2019

Copy link to clipboard

Copied

You need to store the '$result' message in a php $_SESSION variable because you are returning to the same page once the form update button is clicked and the $result variable wont be set until you click the form update button again. Everytime the page reloads php resets itself to zero unless you store information in session variables.

Give me a shout in the forum if you want a code example using a $_SESSION

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2019 Apr 23, 2019

Copy link to clipboard

Copied

Dear osgood_,

I really appreciate your response.

Please I will need some code example on how to use the $_SESSION.

Thank you so much....

Mike

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 23, 2019 Apr 23, 2019

Copy link to clipboard

Copied

Start a php SESSION by inserting session_start(); BEFORE anything else on your page:

<?php

session_start();

?>

Obviously you then have a connection to your database.

Below is the code to execute once the 'submit' button has been clicked: (include the correct variable id name for your $_POST - $***_id=$_POST['***_id']; and your sql query WHERE ***_id=$***_id in the code below)

<?php

if(isset($_POST['submit'])) {

$***_id=$_POST['***_id'];

$txpaid=$_POST['txpaid'];

$editedby=$_POST['editedby'];

$dateedited=$_POST['dateedited'];

$qur="UPDATE tbl_assess SET txpaid='$txpaid', editedby='$editedby', dateedited='$dateedited' WHERE ***_id=$***_id";

mysql_select_db($database_DB, $DB);

$retval = mysql_query($qur, $DB);

if($retval) {

$_SESSION['result'] = '<div class="alert alert-success">Your assessment was successfully posted</div>';

}else {

$_SESSION['result'] = '<div class="alert alert-danger">Sorry there was an error posting your assessment. Please try again</div>';

}

mysql_close($DB);

}

?>

Insert the below in the page where you want your 'result' message to appear:

<?php

if(isset($_SESSION['result'])) {

echo $_SESSION['result'];

unset($_SESSION["result"]);

}

?>

The other way to do this is use Ajax to process the form and send the response 'data' from the php processing page back to the form page.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 24, 2019 Apr 24, 2019

Copy link to clipboard

Copied

Dear osgood_​,

Thank you so much for the answer.

It worked perfectly well although I had to remove this closing like "mysql_close($DB);" due to the "Warning: mysql_query(): 3 is not a valid MySQL-Link resource......" it was generating. Besides this, everything is OK.

Thank you so much.

Mike

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

LATEST

there was question, that seams to have been removed ?....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines