Asked  8 Months ago    Answers:  5   Viewed   33 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.

  <?php mkdir('/test1/test2', 0777, true); ?>
  return false;



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.


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

test.php FILE:

 <?php mkdir('/test1/test2', 0777, true); ?>
Wednesday, March 31, 2021
answered 8 Months ago

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.

Friday, May 28, 2021
answered 5 Months ago

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 ;-) )

Saturday, May 29, 2021
answered 5 Months ago

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.

Saturday, May 29, 2021
answered 5 Months ago

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/>


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


Tuesday, August 10, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :