2

HTML

<input id="box1" type="submit">

$('#box1').click( function() {
 $.ajax({
    url:"insert.php",
    type:"POST",
});
});

PHP insert.php

$link = mysqli_connect("localhost", "root", "", "votingcount");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// attempt update query execution
$sql = "UPDATE vote_result SET vote_count = vote_count + 1 WHERE   photo_no=1";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

When the submit button (#box1) is clicked, the UPDATE statement would increase the vote count in SQL by 1. After running the insert.php, at the same time a overlay would appear to show the CURRENT vote count from the SQL, I would want the overlay box content to display the int vote count with the following sql statement : $sql = "SELECT vote_count FROM vote_result WHERE photo_no=1";

How would I allow the .click function for #box1 be able to run another php where I could retrieve the value and update the #clicked value in the following overlay?

<div id="myModal" class="modal">

    <!-- Modal content -->
    <div class="modal-content">
        <span class="close">&times;</span>
        <img src="tyedit.jpg" height="1024" width="724" />
            <div id="imagine">
                <span id="clicked">0</span><br/>
                <span id="word">VOTES</span>
            </div>
    </div>

    </div>
3
  • 1. In the same insert.php file, after the update - select the required value and instead of "Records aded succ.." echo that value. 2. add a "success" event to your ajax code that takes the returned value and append it to wherever you want. Commented Feb 7, 2017 at 14:27
  • @OfirBaruch $sql = "SELECT vote_count FROM vote_result WHERE photo_no=1" Do you mean to insert that right after the update? My html is a separate file from the insert.php, how would I be able to append the value? Commented Feb 7, 2017 at 14:31
  • Follow @bipin's answer. Commented Feb 7, 2017 at 14:32

2 Answers 2

1

Why don't you run your select query after the insert query in insert.php itself. After successful update query, just run another query to select the vote count from your table.

$sql = "SELECT vote_count FROM vote_result WHERE photo_no=1";

if($result = mysqli_query($link, $sql)){
    echo mysqli_fetch_assoc($result)['vote_count'];
} 

The echoed statements are you ajax response, so use callback function on success to update your overlay.

on your ajax call

$.ajax({
   url:"insert.php",
   type:"POST",
   success:function(response){
       $("#clicked").html(response);
   }
});

also if you wish to have multiple echoes on your insert.php file you could get a json response using json_encode() to all your echoes.

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

4 Comments

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean I had this error when displaying the response.
@user2728875 have you tried my answer to solve it ??
make sure your sql is a select query, insert, update or delete query returns boolean - true upon successful modification in database table, false otherwise .... however on select query it returns a mysqli_result as you are fetching data ... i can only imagine that you probably ran mysqli_fetch_assoc() on an update query ??
Hey @sujanbasnet, I've managed to solve it. Thanks alot for your help!
0

first change type submit to button

<input id="box1" type="button" value="click">

than click event call ajax

<script>
$('#box1').click( function() {
    $.ajax({
       url:"insert.php",
       type:"POST",
       success: function(data) {
          $("#myModal").modal('show');
          $('#clicked').text(data);
       }
    });
});
</script>

Change php code like this

$sql = "UPDATE vote_result SET vote_count = vote_count + 1 WHERE   photo_no=1";
if(mysqli_query($link, $sql)){

   $sql1 = "SELECT vote_count FROM vote_result  WHERE photo_no=1";
    $result = mysqli_query($link, $sql1);
    $row = mysqli_fetch_assoc($result);
    echo $row["vote_count"];

} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($sql);
}

1 Comment

You don't need the while loop in this case.

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.