php / javascript / ajax submit form > save data to database > update data on website
I have 3 tasks :
1) Submit the message in a textarea when the user press enter button on keyboard or click "submit" button.
2) Use php/mysql to save the message onto database.
3) After save it onto database, need to show the updated message onto the website back.
The problem is... There is no php script that can change "Enter" button event, so I need to use javascript, if I use javascript, I need to create a new page sendMsgToDB.php to submit the data to database by the following javascript/ajax :
<script type="text/javascript">
function gg(e) { //sumbit the form when user press Enter button
key = e ? e.which : window.event.keyCode;
if(key==13) {
$.ajax({
type: "POST",
url: "sendMsgToDB.php?msgid=" + msgid,
data: dataString2,
success: function() {
$('textarea.resposting').val(''开发者_开发问答);
}
});
}
}
$(function() {
document.getElementById("resposting").onkeypress = gg;
});
</script>
I use mysql to retrieve 10 latest messages from database and use pure php to display the messages onto the website. If I want to show updated messages onto website after the user submit the data through the textarea by pressing "Enter" button on keyboard, sure I need to call the function Display_New_Msg()
after the data is complete send to database. If I use Javascript/ajax to sumbit data, how to use javascript/ajax/jquery determine whether or not the data is complete send to database?
Seriously I don't like to use javascript/ajax/jquery, but I force to use them because I need to change the "enter" button event, is there a way to change "enter" button event by using php? How to make the press "enter" button act like click "submit" button to submit the form <form name="frmMsg" action="currentPage.php">
, the action will go to the currentPage.php, so the page is refresh, so it will re-execute the function Display_New_Msg()
automatically to show the new message, so don't need to dertermine whether or not the data is complete send to database anymore.
If I use Javascript/ajax to sumbit data, how to determine whether or not the data is complete send to database?
It all comes down to the response from the resource that's handling the AJAX request. In this case, sendMsgToDB.php
. That script needs to make sure it's doing what it needs to do and that everything's getting into the database (usually a lack of error is sufficient), then sending a response accordingly. If there is an error on the server, return an HTTP error code (such as a 500 server error) to indicate this.
The jQuery .ajax()
call then determines the call-back function to use based on the response from the server. Your code currently handles a successful response:
success: function() {
$('textarea.resposting').val('');
}
In order to handle an error response, you'd want a second callback for error
:
error: function() {
alert('There was an error!');
}
The HTTP response codes exist for exactly this purpose, so that clients consuming those responses can easily determine the status of the request.
Edit: Conversely (and perhaps more to the spirit of your question, if I'm now re-interpreting you correctly), you can use the jQuery submit()
function to submit the form in your key press logic. That way you don't have to use AJAX and can just use a regular form submission and response, which you indicate to be your preference. Something like this:
if(key==13) {
$('#myForm').submit();
}
精彩评论