0

I want to execute sql command such as

$query_run = mysql_query($query);

in php. But the only way I can find to trigger php event is to submit a form using:

if(isset$POST['submit']) {
    $query_run = mysql_query($query);
}

But this would refresh the page and wipe off every thing that dynamically created on the page.

What I want is to have a button, press which to trigger sql commands without reloading the page like onclick in js. However, sql command cannot be run inside js function (as I can find).

2

2 Answers 2

1

PHP is parsed server side which means you can't have it run code without making a server request. Once the page is delivered to the client any php parsing is complete.

You can create a button or link that will send off an http request through javascript, an AJAX call, to a small php script that does the database work you want done. You wouldn't have it call your whole page again. The output from this gets returned to the javascript and can be used to update a CSS block element with success or failure notices.

Here's the W3C Ajax tutorial http://www.w3schools.com/ajax/default.asp

Sign up to request clarification or add additional context in comments.

Comments

1

What you need to do is an AJAX call. This requires Javascript (preferably with the help of a library such as jQuery) to assemble the date from the page, make the call to the PHP page, and to present the results onto the page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.