-1

I inserted an onclick to my button but don't know where I'm going wrong here but it seems like the function is fired each time the page is loaded.

How can I call the function ONLY when clicking on the button itself

<button type="submit" class="btn btn-primary" onclick="<? $query = DB::update('ads')->set(array('sale_agreed' => '999'))->where('id_ad', '=', $msg_thread->ad->id_ad)->execute(); ?>"><?=_e('ACCEPT This Offer')?></button>  

Any take on this? Thanks ;-)

David G
  • 39
  • 6
  • You can not call php function like you have done. You should get basic knowledge about jquery/javascript – Pankaj Makwana Jul 19 '17 at 10:17
  • Use cannot call server side function from client side in this way, You should use AJAX and JS – Shubham Agrawal Jul 19 '17 at 10:19
  • 1
    just submit your form and in backend perform your db operation – RAUSHAN KUMAR Jul 19 '17 at 10:20
  • I don't know why the question is closed !. replace your code with this ` ` – Accountant م Jul 19 '17 at 10:27
  • @Accountantم Looks like you better to read the duplicate article too ... – Teemu Jul 19 '17 at 10:33
  • @Teemu Looks like you didn't get my code – Accountant م Jul 19 '17 at 10:42
  • @Accountantم What's the point? All that PHP is executed before the document is even delivered to a browser. – Teemu Jul 19 '17 at 10:48
  • @Teemu You are just confused because his PHP code is inside strange place - the `onclick` .He wanted to execute the PHP code **only** if the button is clicked,so I added a condition before executing the PHP code to check if the button is clicked or not, if the button is clicked then execute his PHP code. I know his PHP code Is in client side code place, but this should not confuse a user with rep like you. you better be focused when reading the problem. EDIT: I also added a parameter in the button so the server can check – Accountant م Jul 19 '17 at 10:56
  • 1
    @Accountant > I have a feeling that you're on the right track with this rather simple solution although it doesn't seem to be working for me yet... I also tried with statement *if (isset($_POST* instead of *!empty($_GET* but still won't do it for me don't know why... Any further input? – David G Jul 19 '17 at 11:24
  • @DavidG What is the method of the`< form>` of your button? if POST , then change the condition to `!empty($_POST['buttonClicked'])` . – Accountant م Jul 19 '17 at 11:26

4 Answers4

0

You can't use php functions from client side. The attribute "onclick" fires a javascript funciont, not a php one. In order to execute a php function with onclick, you have to make an Ajax request to the server.

$('.button').click(function() {
    $.ajax({
      method: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
   })
    .success(function( msg ) {
         alert( "Data Saved: " + msg );
      });
});

In the "url" variable you have to put the url to your php script, and the data object contains all the PHP $_REQUEST parameter to be send to the script. The success function executes once the script is complete.

Daniele Fois
  • 121
  • 6
0

Button click is client side whereas PHP is server side, but you can achieve this by using AJAX.

$('.button').click(function() {

    $.ajax({
        type: "POST",
        url: "some.php",
        data: { name: "John" }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });

});

In your PHP file:

<?php
    function abc($name){
        //your code here
    }
?>
xxx
  • 1,153
  • 1
  • 11
  • 23
Bug Inspector
  • 301
  • 4
  • 15
0

You can make use of jQuery Ajax to perform this operation. Add a button with some id.

<button id="click-button"></button>

Inside your script tag.

$(document).read(function(){
    $("#click-button").click(function(){
        $.ajax({
        url: "remote-file.php",
        method:"POST",
        data:"token=buttonclick",
        success: function(result){
            if(result != "fail"){
                //Perform actions with the results...
            }
        }});
    });
});

In you PHP remote-file.php

<?php
if(isSet($_POST['token']) && $_POST['token'] == 'buttonclick'){
    $result = myFunction();
    echo $result;
}else{
    echo "fail";
}

function myFunction(){
    // Perform your DB actions...
    return true; //Return your data
}
?>
Nandan Bhat
  • 1,573
  • 2
  • 9
  • 21
-2

You cant add php functions to client side button clicks.

You should get your PHP to check if the page has been submitted and then run your function