Mechaworx
11-09-2010, 12:38 AM
Hi all,
I'm very new to php and jquery and ajax but I keep learning and I hope someone can help me.
I have implemented a blog commenting system on my web site to enable users to post comments to blog articles along with form validation. I have had success doing this with php
But of course with php the page has to reload in order for the comment to show. I am trying to implement Jquery's ajax method in order to try to get comments to load without reloading the page.
I am using Jquery Library version 1.4.2. When I try to implement the jquery code, something in process.php script causes jquery1.4.2.js file to throw an error on line 5252.
Line 5252 in the jquery library file is this:
try {
5252 xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
Hope that makes sense.
Here is the jquery code I'm trying to implement.
$(function() {
$('div.cform input#submit').click(function() {
$('div.cform').append('<img src="../images/pageElements/ajax-loader.gif" class="loaderIcon" alt="Loading..."/>');//this will simulate processing by loading an animated gif
var name = $('input#name').val();
var email = $('input#email').val();
var website = $('input#website').val();
var comments_body = $('textarea#comments').val();
var aid = $('input#id').val();
$.ajax({
type: 'post',
url: 'process.php',
data: 'name=' + name + '&email=' + email + '&website=' + website + '&comments=' + comments_body + '&id=' + aid,
success: function(results) {
$('div.cform img.loaderIcon').fadeOut(1000);//fade out processing simulation image
$('ul#response').html(results);//display succcess or failure errors.
}
}); // end ajax
});
});
When using Firebug, the form is processed by jquery but the information doesn't seem to be passed off to process.php because the data is not added to the database table.
Here's my php script which functions without implementing Jquery
<?php
mysql_connect("localhost", "root", "m3gatr0n");
mysql_select_db("masscic");
//initialize the variables for the form if users want to post a comment
$name ='';
$email ='';
$website ='';
$comments ='';
$id ='';
$regExp ='';
$errorMsg ='';
if (isset($_POST['submit'])) {
//grab the form data
$curnum = 0; //will itemize the number of errors we have
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comments = $_POST['comments_body'];
$id = $_POST['aid'];
//do some injection cleaning
$name = stripslashes($name);
$email = stripslashes($email);
$website = stripslashes($website);
$comments = stripslashes($comments);
$name = strip_tags($name);
$email = strip_tags($email);
$website = strip_tags($website);
$comments = strip_tags($comments);
$regExp = '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/';
//check for errors
if (strlen($name) < 2) {
$curnum ++;
$errorMsg['name'] = '<span style="color:#ff0000">'.$curnum.'. Your name is required</span>';
}
if (!preg_match($regExp, $email)) {
$curnum ++;
$errorMsg['email'] = '<span style="color:#ff0000">'.$curnum.'. Please enter a valid email address</span>';
}
if (strlen ($comments) < 3) {
$curnum ++;
$errorMsg['comments_body'] = '<span style="color:#ff0000">'.$curnum.'. You need to post a comment</span>';
}
$name = mysql_real_escape_string($name);
$email = mysql_real_escape_string($email);
$website = mysql_real_escape_string($website);
$comments = mysql_real_escape_string($comments);
//done with error checking now perform the insert
if (!$errorMsg) {
$sqlInsert = mysql_query("INSERT INTO comments(aid, name, email, website, comments_date, comments_body) VALUES('$id','$name','$email','$website', now(), '$comments')") or die (mysql_error());
echo '<li class="success"> Congratulations, ' . $name . '. Your comment was posted successfully! </li>';
}
else {
$response = (isset($errorMsg['name'])) ? "<li>" . $errorMsg['name'] . "</li> \n" : null;
$response .= (isset($errorMsg['email'])) ? "<li>" . $errorMsg['email'] . "</li> \n" : null;
$response .= (isset($errorMsg['comments_body'])) ? "<li>" . $errorMsg['comments_body'] . "</li>" : null;
echo $response;
} # end if there was an error sending
}
?>
Sorry this is so long but there's a lot of stuff going on as you can see.
Thanks for any information.
I'm very new to php and jquery and ajax but I keep learning and I hope someone can help me.
I have implemented a blog commenting system on my web site to enable users to post comments to blog articles along with form validation. I have had success doing this with php
But of course with php the page has to reload in order for the comment to show. I am trying to implement Jquery's ajax method in order to try to get comments to load without reloading the page.
I am using Jquery Library version 1.4.2. When I try to implement the jquery code, something in process.php script causes jquery1.4.2.js file to throw an error on line 5252.
Line 5252 in the jquery library file is this:
try {
5252 xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
Hope that makes sense.
Here is the jquery code I'm trying to implement.
$(function() {
$('div.cform input#submit').click(function() {
$('div.cform').append('<img src="../images/pageElements/ajax-loader.gif" class="loaderIcon" alt="Loading..."/>');//this will simulate processing by loading an animated gif
var name = $('input#name').val();
var email = $('input#email').val();
var website = $('input#website').val();
var comments_body = $('textarea#comments').val();
var aid = $('input#id').val();
$.ajax({
type: 'post',
url: 'process.php',
data: 'name=' + name + '&email=' + email + '&website=' + website + '&comments=' + comments_body + '&id=' + aid,
success: function(results) {
$('div.cform img.loaderIcon').fadeOut(1000);//fade out processing simulation image
$('ul#response').html(results);//display succcess or failure errors.
}
}); // end ajax
});
});
When using Firebug, the form is processed by jquery but the information doesn't seem to be passed off to process.php because the data is not added to the database table.
Here's my php script which functions without implementing Jquery
<?php
mysql_connect("localhost", "root", "m3gatr0n");
mysql_select_db("masscic");
//initialize the variables for the form if users want to post a comment
$name ='';
$email ='';
$website ='';
$comments ='';
$id ='';
$regExp ='';
$errorMsg ='';
if (isset($_POST['submit'])) {
//grab the form data
$curnum = 0; //will itemize the number of errors we have
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comments = $_POST['comments_body'];
$id = $_POST['aid'];
//do some injection cleaning
$name = stripslashes($name);
$email = stripslashes($email);
$website = stripslashes($website);
$comments = stripslashes($comments);
$name = strip_tags($name);
$email = strip_tags($email);
$website = strip_tags($website);
$comments = strip_tags($comments);
$regExp = '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/';
//check for errors
if (strlen($name) < 2) {
$curnum ++;
$errorMsg['name'] = '<span style="color:#ff0000">'.$curnum.'. Your name is required</span>';
}
if (!preg_match($regExp, $email)) {
$curnum ++;
$errorMsg['email'] = '<span style="color:#ff0000">'.$curnum.'. Please enter a valid email address</span>';
}
if (strlen ($comments) < 3) {
$curnum ++;
$errorMsg['comments_body'] = '<span style="color:#ff0000">'.$curnum.'. You need to post a comment</span>';
}
$name = mysql_real_escape_string($name);
$email = mysql_real_escape_string($email);
$website = mysql_real_escape_string($website);
$comments = mysql_real_escape_string($comments);
//done with error checking now perform the insert
if (!$errorMsg) {
$sqlInsert = mysql_query("INSERT INTO comments(aid, name, email, website, comments_date, comments_body) VALUES('$id','$name','$email','$website', now(), '$comments')") or die (mysql_error());
echo '<li class="success"> Congratulations, ' . $name . '. Your comment was posted successfully! </li>';
}
else {
$response = (isset($errorMsg['name'])) ? "<li>" . $errorMsg['name'] . "</li> \n" : null;
$response .= (isset($errorMsg['email'])) ? "<li>" . $errorMsg['email'] . "</li> \n" : null;
$response .= (isset($errorMsg['comments_body'])) ? "<li>" . $errorMsg['comments_body'] . "</li>" : null;
echo $response;
} # end if there was an error sending
}
?>
Sorry this is so long but there's a lot of stuff going on as you can see.
Thanks for any information.