Reputation: 3159
I have suggestion.php form as follow and call the ajax method. I always got "Thanks you" alert but post data never insert into db. I am very sure that my connection string is correct because it is sometime insert the record but most of the time it is not inserted.
Any help, please. Regards, Alex
suggestion.php
<form class="suggestion" role="form" method="POST" action="suggestion_add.php">
<div class="form-group"><label name="warnning" id="urlwarnning">Currently we only support YouTube as well as Vimeo videos</label></div>
<div class="form-group"><input name="url" id="url" type="text" class="form-control" placeholder="URL" required></div>
<div class="form-group"><input name="title" id="title" type="text" class="form-control" placeholder="Title" required></div>
<div class="form-group"><textarea id="desc" name="desc" class="form-control" rows="5" placeholder="Description.." required></textarea></div>
<div class="form-group"><input name="name" id="name" type="text" class="form-control" placeholder="Name" required></div>
<div class="form-group"><input name="email" id="email" type="text" class="form-control" placeholder="Email ID" required></div>
<button class="btn btn-lg btn-primary btn-block" type="submit">SUBMIT</button>
</form>
Function
$(function () {
$(document).on('submit', ".suggestion", function(e) {
var name=$("#name").val();
var email=$("#email").val();
var url=$("#url").val();
var title=$("#title").val();
var desc=$("#desc").val();
var formURL = $(this).attr("action");
var youtube_regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var vimeo_regExp = /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/;
var match = url.match(youtube_regExp);
if (match&&match[2].length==11){
var vid = match[2];
var src = "youtube";
var check = 1;
}else{
var match = url.match(vimeo_regExp);
if (match){
var vid = match[3];
var src = "vimeo";
var check = 1;
}else{
alert("not a valid url");
var check = 0;
}
}
if(check == 1){
var data="title="+title+"&desc="+desc+"&src="+src+"&vid="+vid+"&name="+name+"&email="+email;
$.ajax(
{
url : "suggestion_add.php",
type: "POST",
data : data,
success:function(data, textStatus, jqXHR)
{
$("#name").val("");
$("#email").val("");
$("#url").val("");
$("#title").val("");
$("#desc").val("");
alert("Thank you !");
},
error: function(jqXHR, textStatus, errorThrown)
{
alert(errorThrown);
}
});
}else{
alert("Check again!");
}
e.preventDefault();
});
});
suggestion_add.php
<?php
include("config.php");
$con=mysql_connect($db_host,$db_uname,$db_pwd,$db_name);
mysql_select_db($db_name);
$src=$_POST['src'];
$name=$_POST['name'];
$email=$_POST['email'];
$title=$_POST['title'];
$desc=$_POST['desc'];
$vid=$_POST['vid'];
$query="INSERT INTO `suggestion` (`id`,`src`,`vid`,`title`,`desc`,`name`,`email`)VALUES (NULL,'$src','$vid','$title','$desc','$name','$email');";
$result=mysql_query($query);
?>
Upvotes: 0
Views: 2525
Reputation: 3159
Thanks you so much for all of your suggestion. It is happened because of the Single quote
.
After I have added $dbc->real_escape_string($title)
it is working perfectly.
Upvotes: 0
Reputation: 5847
Success will be called as long as the script suggestion_add.php
runs.
I recommend that you print something at the end of the script, like:
header("Content-type: application/json");
die(json_encode(array("success" => $result)));
// Where $result is true if query is OK else false.
// If you want the errror message to be passed to the javascript
// function too you can add "error" => $error to the array too,
// and then print it in the 'success' function.
Then in your 'success' function check:
success:function(data){
if(data.success){
//Do success
} else {
//Do failure
}
}
Also, do not use the deprecated mysql_*
functions, check out mysqli
or PDO
for better APIs which both have prepared statements, so that you do not open up for sql-injection vulnerabilities.
Edit:
You don't have to set up the data as a post request when using the jquery ajax function, you can do:
var data = {
"title": title,
"desc": desc,
"src": src,
"vid": vid,
"name": name,
"email": email
}
And then just pass the object data
as the data
parameter.
Edit2:
If you do not know how to, to get the mysql error from the old deprecated mysql_*
api, you use the mysql_error function.
Something like:
$result = mysql_query($query) or die(array("success" => false, "error" => mysql_error());
Upvotes: 2
Reputation:
Could it be that you have setup a unique index on the table and sometimes mysql cannot insert a duplicate key?
You can do this to see if there are errors with mysql:
$result=mysql_query($query) or die('Query failed: ' . mysql_error());
UPDATE:
$con = mysql_connect($db_host,$db_uname,$db_pwd) or die (mysql_error());
mysql_select_db($db_name, $con);
Upvotes: 0
Reputation: 2424
I thing your data is not passing to php file because you are postting the data twice(inside form tag)
action="suggestion.php"
and in jQuery ajax
$.ajax(
{
url : "suggestion_add.php",
type: "POST",
data : data,
success:function(data, textStatus, jqXHR)
{
}});
better use anyone to post the value.. change form tag and try
<form class="suggestion" >
</form>
i think it will work..
Upvotes: 0