0

I'm a newbie in php and I need help with this. The query runs every time on page load and not when I click my button.

<input type="button" name="button" onclick="<?php 
                  $temp_id = $row_RecStudent['stu_id'];
                  mysql_query("UPDATE tbl_studentdetail SET stu_queue = 0 WHERE stu_id = $temp_id"); ?>" value="Unqueue" />

This is so frustrated because I run this button in a loop but every time the button loads it automatically run the mysql_query inside not when I click it.

SOLVED! Check on AnthonyB's answer.

4
  • you can't attach a PHP code to a javascript onclick event. instead the onclick can trigger an ajax call to run the update Commented Nov 29, 2016 at 7:06
  • you can follow : stackoverflow.com/a/37270558/7063928 Commented Nov 29, 2016 at 7:09
  • that is not how this works: all the PHP is executed before anything is sent to the browser, and all the javascript is executed after everything is sent to the browser. you should read some tutorials about how webservers work. and you shouldn't keep on using mysql-functions, they are deprecated and in PHP7 removed. use mysqli or PDO Commented Nov 29, 2016 at 7:13
  • Thanks for the replies. Indeed I knew nothing about this previously and now I've learnt a lot more in these hours compared to last few days. Commented Nov 29, 2016 at 9:14

3 Answers 3

3

The query runs every time on page load and not when I click my button.

Which is because onclick events are usually executed in the browser with client side scripting, which invariably means javascript. What you really need to have is

 <input type="button" name="button" onclick="js_function()" value="Unqueue" />

Where js_function is a javascript that will make an ajax request to the page or simply cause the form to be submitted. What you are doing is making onclick equal to the result of the PHP code fragment, which obviously happens at the server side and has absolutely nothing at all to do with the user clicking on the button.

Sign up to request clarification or add additional context in comments.

3 Comments

PHP is a server side language. That means it is executed once, just before return HTML to client. JavaScript is here a client side language. That means it is executed by the browser (not by the server). In your case, PHP execute the query "UPDATE ...", and then return html. What you want, as said by @e4c5, is make an Ajax request to do this. An AJAX request will send an HTTP request to your server, and your server will execute update query.
thank you @AnthonyB for the comment. e4c6 is the Caro-Kann while my preferencce is for the Sicilian
I did'nt understand chess reference, but I fixed my comment. You could add example of AJAX request (perhaps using jQuery) with the server side code, in order to your answer be more useful.
1

You should use an AJAX request, because there is no redirection nor reloading. To use the exemple below, you must include jQuery

<!-- data-id is the id you got before -->    
<input type="button" data-id="<?php echo $row_RecStudent['stu_id']; ?>" name="button" class="button-on-click" value="Unqueue" />

<script type="text/javascript">
    $(function() {
        //On click on this kind of button
        $(".button-on-click").on('click', function() {
            var id = $(this).attr('data-id');
            //Send AJAX request on page youraction.php?id=X where X is data-id attribute's value
            //the ID used in mysql query
            $.ajax({
                url: "youraction.php",
                method: "GET",
                data: {
                    id: id
                }
            }).done(function() {
                //Once it is done
                console.log("OK!");
            });
        });
    });
</script>

On youraction.php

<?php
//Cast $_GET['id'] to integer to prevent SQL injection.
//If there is no id given, stop
if (empty($_GET['id'])) {
    exit();
}
$id = (int)$_GET['id'];
mysql_query("UPDATE tbl_studentdetail SET stu_queue = 0 WHERE stu_id = $id");

Be careful, mysql_* functions are deprecated. Instead, use mysqli_* functions or PDO. And you could prefere use prepared statement for security reason.

8 Comments

I appreciate this so much. But there an issue at line data: {
Indeed I fixed it. There were errors because of parenthesis and a coma in data. Now that should work.
good answer +1 from me for including an ajax sample and for the warning about mysql_*
Thanks @e4c5! I was inspired by your answer.
Your code is quite clear but I still don't manage to get it to work. I've been rechecking but when I click on button just nothing happen.
|
0

Please note that you're supposed to mention a Javascript function for onClick event. You can't write PHP script here.

One way to overcome this problem is create a separate PHP file which will contain the query.

// page1.php

<a href="action.php?stu_id=<?php echo $row_RecStudent['stu_id'] ?>">Unqueue</a>

// action.php

<?php 
    $temp_id = $_GET['stu_id'];
    mysql_query("UPDATE tbl_studentdetail SET stu_queue = 0 WHERE stu_id = $temp_id"); 
    header("Location:page1.php");
?>" 

1 Comment

Using a link is not a bad idea, instead of using an AJAX request. But it cause a reloading of the page. If this is not a problem, you can use it. But some times, a reloading is a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.