2

I would like to implement a pop-up request for the user , e.g. if the user press "yes" for confirm data on MySQL DB are modified otherwise no. This should be done without creating another php confirmation page. I looked through forum discussions , I founded some possible solutions based on Ajax or Javascript but unfortunately I don't know how to programme with these tools. Can anyone helps? many thanks

//$sql_select= "SELECT .... FROM DB
} 
//------------------Button save data pressed--------------------
if (isset($_POST['bottone_update'])) {
// If te user click confirm button I have to modify the database
    /*$sql_upd = "UPDATE db_sale.prenotazioni_alpha SET VALUES.....*/
    }   
?>

<html>

<head>
</head>

<style type='text/css'>
    <meta charset="utf-8">
</style>

<body>

<form method="post" action="">
    <!--Table data -->
    <table class='table1' id="pos_table1">
        <tr>
            <td><textarea name="alpha_9_10"></textarea>
                <td>
                    <td><textarea name="meda_9_10"></textarea>
                        <td>
        </tr>
        <tr>
            <td><textarea name="alpha_10_11"></textarea>
                <td>
                    <td><textarea name="meda_10_11"></textarea>
                        <td>
        </tr>
    </table>
    <button type="submit">look for data</button>
    <button type="submit">Save data</button>

ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
Valgo54
  • 75
  • 3
  • look at this http://stackoverflow.com/questions/9334636/javascript-yes-no-alert/9334685 if you not sure what to do let me know. – Maytham Fahmi Feb 28 '16 at 11:34

2 Answers2

2

You can simply add an onSubmit attribute to the form, with the JavaScript function confirm in it. It will open a pop-up in the browser, asking whatever you define within the first parameter, with two buttons: "OK" and "Cancel". This has the feature of returning true or false (a boolean), when clicking "OK" or "Cancel" respectively.

This means that if you do onSubmit="return confirm('Are you sure?');" in the form, you'll be able to send the form only if you press "OK" to this check. Then, in PHP, you just check weather or not the form has been submitted; and if it has - you perform your update-query, like you already started doing - no need for additional checks!

if (isset($_POST['bottone_update'])) {
    // Perform your query
}

Your opening <form>-tag should then contain this:

<form method="POST" onSubmit="return confirm('Are you sure?');">
    <!--- Rest of form goes here -->
</form>

If you have action="", you can just remove it altogether.

Qirel
  • 25,449
  • 7
  • 45
  • 62
0

Try This:

$('#button').click(function () {
    if (confirm('Are You Sure?')) {
        $.post('http://localhost/ajax.php', function () {
            alert('Data Added Successfully!');
        });
    }
});