0

I have this submit button without a form..

<input type='submit' id='conx' name='X' value='TEST x'>

Now when the button is clicked i need to execute this code.

$con = fopen("/tmp/myFIFO", "w");
fwrite($con, "XcOn");
close($con);

How can i execute it in jquery and ajax?.

$("#conx").click(function(){

//Execute this code
//$con = fopen("/tmp/myFIFO", "w");
//fwrite($con, "XcOn");
//close($con);

});

Thanks.

8 Answers 8

1

Post that to a PHP page with Ajax and execute those ther

$("#conx").click(function(){
  $.post("yourPHPPageWithMagicCode.php");
});

Make sure yo have that PHP code in yourPHPPageWithMagicCode.php file.

If you want to show a response after the process is done, you can return something from your PHP page and let the callback of $.post handle it.

In your PHP page after your code,put an echo

echo "successfully finised";

Now change the jquery code to handle the callback

$("#conx").click(function(){
  $.post("yourPHPPageWithMagicCode.php",function(repsonse){
    alert(response);
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Let's say your PHP file is called write.php. I believe you can do this:

  $("#conx").click(function() {
    $.ajax({
      url: "/write.php"
    });
  });

Comments

0

All in one file.

test.php

<?php
if ($_POST['cmd'] === 'ajax') {
    $con = fopen("/tmp/myFIFO", "w");
    fwrite($con, "XcOn");
    fclose($con);
    exit;
}
?>

<!doctype html>
<head>
    <meta charset="utf-8">
    <title>meh</title>
</head>
<body>


<input type="submit" id="conx" name="X" value="TEST x">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function(){
    $("#conx").bind('click', function(){
        $.post("test.php", { cmd: "ajax" } );
    });
});
</script>

</body>
</html>

2 Comments

Thanks a lot great answer. What if i have 3 button?.. can i just add it on the top?.. e.g. name of button is X,Y and Z
That would be an easy edit. And it can be done in a number of ways. You could add a new jquery bind() for each button with the $.post parts cmd having different value for each button. Then you would change the php if to include the other cases. Something like this: codepad.org/F4RGDJUo
0

Add something like below inside your click function to call a PHP script:

if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET", "URL/TO/PHP/SCRIPT",true);
xmlhttp.send();

2 Comments

why not use jQuery when available?
Is there any other way. E.g not creating a extra php file?
0

You need a jquery ajax call between the click event block

$("#conx").click(function(){

    $.ajax({
        url: "phpfile.php",
        type: "post",
        //data: serializedData,
        // callback handler that will be called on success
        success: function(response, textStatus, jqXHR){
            // log a message to the console
           alert(response);
        }
    });

});

put whatever code you want in the phpfile.php file.

2 Comments

Can it be done without doing this or any other way?.Rather than creating another php file? thanks
No, this is the only way to do PHP work in javascript events.
0

You need to load the 'click event' on the document load i.e.:

$(document).ready(function($){
     $("#conx").click(function(){ .....

Comments

0

one way is

$("#conx").click(function(){
    $.post("PHPFILENAME.PHP",{
        whatdo:otherstuff
    },function(d){
        // return d here
    }
})

run your code in the php file.

Comments

0

You could call a javascript function from the button:

<input type='submit' onclick="myFunction()" id='conx' name='X' value='TEST x'>

and have a page load in your function:

function myFunction(){
    var loadUrl = "magic.php";  
    var result = $("#result").load(loadUrl);  
}

Then have magic.php run your method:

//Execute this code
//$con = fopen("/tmp/myFIFO", "w");
//fwrite($con, "XcOn");
//close($con);

This is all untested pseudo-code...

2 Comments

Thanks, but is it possible to not create another php file?.. I need something that will not read another php file. eg execute everything in the current php file.
You have to make another request to the webserver to do any work, but you could access the existing php page. You could have a querystring that causes the same file to do a different activity. Something like test.php?logevent=1, then have a simple if statement at the top of test.php that looks for logevent and does your special code instead of its normal activity

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.