简体   繁体   中英

Prevent redirect on form submission and use the POST variables in php

I am working on a messaging system for a networking website wherein I want to use the data that the user enters. Like if the user enters 'Andy' as the recipient I want the php mechanism to use the $_POST['recipient'] variable to retrieve the id of that user and conduct a lookup rather than just going into a javascript function and terminating. I want the javascript function which actually sends the message to get called after the user clicks another button. I am new to PHP and don't understand the inner workings that well. I have given a small sample code below that is similar to mine but easier on the eyes.

<form>
To:<input type="text" id=rec">
<input type="submit" value="confirm" id="confirm" name="confirm">
<button type="button" id="sub1" onclick="sendPM()">Submit</button>
</form>
<--some mysql look ups using $_POST['rec'] which copies values from the table into the
php variables and stores it in hidden HTML fields to be accessed later by the
sendPM() function-->
$('#sub1').click(function sendPM(){.......}

If you are familiar with JQuery , you may want to use the JQuery .post() method to send a request to another page on your site. Basically, move all of your php to a file called chat.php, and use the JQuery to hit that page and return the results. This would also allow you to do all of the processing without having your user have to refresh the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM