Asked  2 Years ago    Answers:  5   Viewed   72 times

How do i run a PHP function inside jQuery click event. I have the following which is not correct. when the user clicks on a button, i want a new directly created.

$('button').click(function(){
  <?php mkdir('/test1/test2', 0777, true); ?>
  return false;
})

 Answers

5

You cannot run PHP code inside a jquery function. PHP runs on the server-side whereas jquery/javascript runs on the client-side. However, you can request a PHP page using jquery and with the PHP code on that page will run the mkdir that you want.

JS:

$.ajax({
  url: 'test.php',
  success: function(data) {
    alert('Directory created');
  }
});

test.php FILE:

 <?php mkdir('/test1/test2', 0777, true); ?>
Sunday, September 18, 2022
 
4

PHP unlike Javascript runs before it arrives in a users browser, in other words it is executed on the server. Because of this it is not able to respond to interactions on the site by a user on its own.

Option 1: Add an OnClick() event to the anchored link to execute a JS Function and do output time in JS.

Option 2: Use a JS Event on the link or create a JS or jQuery function that executes on button click. Then use AJAX to make a call to the server to execute a PHP function to get the current time and date.

Option 2 is your best bet if you are determined to do it in PHP and the jQuery library makes using AJAX functions pretty easy. But it would be easiest just doing it in JS or jQuery.

Monday, September 12, 2022
 
binw
 
5

You have a syntax error: missing property id.

     $.post("addReading.php", {
                METER_READ: $(this).val(),
                DATE_TAKEN: $(this).val(),
                NOTES: NOTES.val() // I added NOTES:
            }, function (data) {

and your last }); are too much (but that might be a copy/paste error).

Other than that I do not see why it does not work, but I cannot test it without a test case.

You should go on learning how to deal with firebug, though, because I'm sure it would have said you that the property id is missing (firebug is very good at detecting syntax errors, no need for humans in that case ;-) )

Wednesday, November 30, 2022
 
5

Name each date field as if it's being added to a PHP array (not the ids):

<input type="text" id="date'+current+'" name="classDate[]" class="required" />

Using the [ ] syntax (sort of an array_push() shortcut), classDate will automatically become an array in $_POST. Then just do a for loop on that array.

Sunday, November 13, 2022
2

I tried the code of William, Thanks brother.

but it's not working as a simple button I have to add form with method="post". Also I have to write submit instead of button.

here is my code below..

<form method="post">
    <input type="submit" name="test" id="test" value="RUN" /><br/>
</form>

<?php

function testfun()
{
   echo "Your test function on button click is working";
}

if(array_key_exists('test',$_POST)){
   testfun();
}

?>
Saturday, October 8, 2022
 
vimal
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages