开发者

call a php function with a click of a button

开发者 https://www.devze.com 2023-04-13 02:30 出处:网络
how do you call a php function with ajax or javascript or jquer开发者_如何学Pythony? this is my php script

how do you call a php function with ajax or javascript or jquer开发者_如何学Pythony?

this is my php script

function submitmessage() {
    $sent = date(y-m-d-h-m-s);
    $message = $_POST['message'];
    $query = "INSERT INTO chatbox (from,to,message,sent)                            
    VALUES('$fullname','$to','$message','$sent')";
    mysql_query($query) or die(mysql_error());
}


Have the file that the AJAX call connects to be available on the server.

So lets say your function is in the file: functions.inc.php

You could have a php file that includes the functions.inc.php and calls the submitmessage function.

ajax_responder.php:

require_once 'functions.inc.php';    

if(!empty($_POST)) {
    submitmessage();
}

Now you can use the URL to the ajax_responder.php to submit messages.

With jQuery included or written into the page with form:

function submitmessage(){
    $.post("url_path/ajax_responder.php", { fullname: document.chatbox.fullname.value, message: document.chatbox.message.value } );
}

Then, instead of submitting a form, you change your submit button into a button that calls the submitmessage javascript:

<button onclick="submitmessage()">Submit Message</button>

As a side note, for security, I recommend that you do something more with the POST data to ensure that you do not get SQL injection. Since you are already using mysql functions you could try mysql_real_escape_string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号