user3897419
user3897419

Reputation:

Execute PHP function on image onClick without loading the page

I have this HTML code. I want to click on the image and execute the PHP code without loading the PHP page. It will execute in background and update filed in the database.

How can I do this in a reliable way without loading the PHP page?

If I need to use form, how can I use it here? I have tried it but failed.

html code

<table>
<td align="center">
        <div class="round-button">
            <a href="viewStatus.php">
                <img name="myImg" src="images/City.png" alt="Home" />
            </a>
        </div>
    </td>
</table>

php code

<?php

    $connection = mysqli_connect("localhost", "root", "");
    if (!$connection) {
        die("Error: " . mysqli_error());
    }

    $db_select = mysqli_select_db($connection, "myimageda");
    if (!$db_select) {
        die("Error: " . mysqli_error());
    }
    mysqli_query($connection, "UPDATE `myDB` SET `state`=1 WHERE `id` = 1");

    ?>

Upvotes: 2

Views: 22995

Answers (4)

logcat
logcat

Reputation: 485

here is your answer in a simple and easiest way

HTML

<table>
<td align="center">
        <div class="round-button">
            <a href="#" onClick=rec('USE ANY VALUE HERE FOR IDENTITY')>
                <img src="images/City.png" />
            </a>
        </div>
    </td>
    ....
</table>

then use javascript to call that function

<script type="text/javascript">
    function rec(id) {
       $('#newCode').load('viewStatus.php?id=' + id);
    }        
</script>

and write your PHP code like below

<?php

    $connection = mysqli_connect("localhost", "root", "");
    if (!$connection) {
        die("Error: " . mysqli_error());
    }

    $db_select = mysqli_select_db($connection, "myimageda");
    if (!$db_select) {
        die("Error: " . mysqli_error());
    }
    $ver = 'YOUR IDENTITY';
    if($ver == CONDITION) {
       mysqli_query($connection, "UPDATE `myDB` SET `state`=1 WHERE `id` = 1");
    }
    else {
    ...
    }
    ...
    ?>

Upvotes: 2

Arif H-Shigri
Arif H-Shigri

Reputation: 1136

You Cannot Call Php function on an Event. Use Ajax To Send Request To The Server
http://www.ajax-tutor.com/post-data-server.html?userid=JOHNNY

Upvotes: 2

RN Kushwaha
RN Kushwaha

Reputation: 2136

Php cannot be run on client machine hence you should use ajax for that purpose. send your data via ajax on a php page and execute your code on the basis of that data.

use something like this

 <img name="myImg" src="images/City.png" alt="Home" id="img" />

     $("#img").click(function(e){
       $.ajax({
              type:'post',
              data:q,
              url:'ajaxData.php',
              beforeSend: function(){
                    $("#msg").text("sending request to server...");
              },
              complete:function (){
                    $("#msg").text("request received and loaded");
              },
              success:function(result, textStatus, jqXHR) {     
                    $("#ajaxnotifier").html(result);
              },
              error: function(jqXHR, textStatus, errorThrown){
                alert(errorThrown);
              }
         })
     })

You can check the tutorial ajax

Upvotes: 1

Samuele Panarotto
Samuele Panarotto

Reputation: 277

Usually, I use something like this:

<table>
<td align="center">
        <div class="round-button">
            <a href="viewStatus.php">
                <img name="myImg" src="images/City.png" alt="Home" onClick='Func'/>
            </a>
        </div>
    </td>
</table>



<script type="text/javascript">
    function Func() {

        $.ajax({
            type: "GET",
            url: "file.php",
            success:function(json){}, error:function(){}
        }) 
    }
</script>

Upvotes: 2

Related Questions