What would be the best approach to pop up an alert if $_POST['value'] is in the database?
I'm making a script where a user enters a value and clicks Submit. However, I want it to pop up an alert asking if they are sure, if the value is already in the database. I was thinking of doing this with AJAX, but a friend of mine is a jQuery fanatic and told me to go with that instead.
What would be better for this task, and how would I go about doing it in the respective choice? I found a tutorial on Tizag t开发者_如何学运维hat explained how to do something similar, but they said that a submit button wasn't needed, and thus didn't include anything about it.
EDIT: Some code was requested, here it is:
if (isset($_POST['submit'])){
$sql = "SELECT * FROM table WHERE column='$data'";
$query = mysql_query($sql) or die(mysql_error());
$number = mysql_num_rows($query);
if ($number > 0){
//needs to pop up
//if user selects yes {
//continue
//}else{
//break
//}
}
}
Something along these lines, although I know I need to start the AJAX prior to this code or it will have to reload the page.
Ajax is something you can implement with either vanilla javascript or with jQuery, the jQuery makes it really easy though.
Have a look at:
jQuery Ajax Tutorial
The method you should use really depends on what you're doing. It might make more sense to do this server-side.
I'm assuming the user enters this value and hits submit and if the value is not in the database then you will add it or do something else with it. If this is true then do this server-side. Using AJAX in this way won't help the user at all, they still have to go and click the submit button.
If you have your page autocheck the database as the user types...this would benefit the user.
Don't use AJAX just to use AJAX because you like to say you used AJAX
http://en.wikipedia.org/wiki/Ajax_(programming)
精彩评论