0

How to Update mysql using ajax and redirect page after update success ?

When click link update after update mysql redirect to member.php

index.php

<a onClick="update()" id="1">update<a>  

<script type="text/javascript">
    $('.clickable').on('click', function() {
        var data = {
            id: $(this).data('id')
        };
        $.ajax({
            type: 'POST',
            url: 'update.php',
            data: data,
            success: function(response) {
                console.log(response);
            }
        });
    });
</script>

update.php

<?php
include('connect.php'); 
$id = $_POST['id'];
mysql_query ("UPDATE member SET number = '' WHERE id=$id");
?>
<script language="javascript">
    window.location.href = "member.php"
</script>
user3886651
  • 65
  • 1
  • 1
  • 6

3 Answers3

0

First add class to get on click

<a onClick="update();" id="1" class="clickable">update<a> 

Then return response:-

$query = mysql_query ("UPDATE member SET number = '' WHERE id=$id");

if($query)
echo 'success';

Then success

success: function(response) {
    window.location.href = "member.php";
}

So full code will be:-

<a onClick="update()" id="1" class="clickable">update<a>  

<script type="text/javascript">
    function update(){
        $.ajax({
            type: 'POST',
            url: 'update.php',
            data: {id: $(".clickable").attr('id')},
            success: function(response) {
                 //$('.clickable').hide(); // hide not possible on redirect
                 window.location.href = "member.php";
            }
        });
    }
</script>

update.php

<?php
include('connect.php'); 
$id = $_POST['id'];
$query = mysql_query ("UPDATE member SET number = '' WHERE id=$id");
if($query)
  echo 'success';
?>
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

Use AJAX with JSON answer, that way you'll be able to redirect whenever you want without problem, and use objects from PHP to your JS flow:

You don't have to use js events on your DOM, only with your js you'll be able to achieve it, and it'll make your code more reusable, as it'll make it free from hardcoded variables:

index.html

<html>
    <head>
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>

    <body>
        <a class="clickable" href="update.php" id="1">update</a>

        <script type="text/javascript">
            $('.clickable').on('click', function(e) {
                e.preventDefault();

                var link = $(this);
                var data = {
                    id: link.attr('id')
                };

                $.ajax({
                    type: 'POST',
                    url: link.attr('href'),
                    data: data,
                    dataType: 'json',
                    success: function(response) {
                        console.log(response);

                        if (response.result) {
                            console.log('All done!');
                        } else {
                            console.log('Something went wrong!');
                        }

                        if (response.redirect != 'undefined') {
                            window.location.href = response.redirect;
                        }
                    }
                });
            });
            </script>
    </body>
</html>

And then, in your PHP, you'll have to give back the answer as a JSON value, to get it:

update.php

<?php
include('connect.php'); 
$id = $_POST['id'];
$data = array();
if ( mysql_query ("UPDATE member SET number = '' WHERE id=$id") ) {
    $data['result'] = true;
    $data['redirect'] = 'http://example.com/redirect_to_ok_place';
} else {
    $data['result'] = false;
    $data['redirect'] = 'http://example.com/redirect_wrong_result';
}

echo json_encode($data); die();
?>

Note that your AJAX file must finish and echo your response if you want to get the values from PHP, and you MUST have a well formed json, so, if nothing is showed, check with console what values you're getting back, and if you echoed something previously.

To finish, it also important to point out that you MUST NOT USING MYSQL_QUERY PHP FUNCTIONS!! is deprecated, and it's a huge security hole. Use PDO or, if nothing else may be done, mysqli extension. Your code is ready for MySQL injection, so check for it here in SO to avoid it.

Federico J.
  • 15,388
  • 6
  • 32
  • 51
  • i tested but in update.php not get `$_POST[id]` value. and after success your code not redirect but on;y echo text. – user3886651 Jul 29 '14 at 07:27
  • exactly, you're getting text, I suppose something like {result:1;redirect:'http:...'}, which is a JSON object that your JS code will be able to read. Could you paste what are you getting? And I'll modify the js, give me a second... – Federico J. Jul 29 '14 at 07:30
  • My mistake, as update is a link element, you're sending directly to update.php. You have to prevent default behaviour to avoid it, check the updated code – Federico J. Jul 29 '14 at 07:38
  • Another mistake: I forgot to specify that is a JSON request, so your code is able of taking it. I checked it, and you will get the id in your sistem from the POST. If you want to check, just delete from update.php the mysql_query and echo $data json_encoded array, and you'll have it working, XD – Federico J. Jul 29 '14 at 07:47
  • it's work , but when i open link in a new tab it's will access to update.php , how to do with open a new tab? – user3886651 Jul 29 '14 at 08:26
  • that would be client programing: try, instead of window.location.href = response.redirect; using the solution in http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript – Federico J. Jul 29 '14 at 10:05
0

You don't need to make duplicate requests. just change your update.ph as below:

<?php
include('connect.php'); 
$id = $_POST['id'];
mysql_query ("UPDATE member SET number = '' WHERE id=$id");

header('Location: http://www.example.com/member.php'); // Here you need to give your actual path where you want to redirect.

exit;
?>