I am trying to retrieve the ID of the submitted data that the user enters then display it using jQuery/Ajax. The idea is to display the link of the data or, “quote” page. “Your quote was submitted! Click [link] to go to the page” The link would look like this: http://example.com/quote-123. 123 being the ID of the quote.
This is the part I am using to submit the data using ajax and jQuery.
$(document).delegate("'#submit-quote'", "submit", function(){
var quoteVal = $(this).find('[name="quote"]').val();
$.post("add.php", $(this).serialize(), function(data) {
var like = $('.quote-wrap span iframe');
$('.inner').prepend('<div class="quote-wrap group">' + like + '<div class="quote"><p>' + quoteVal+ '</p></div></div>');
// console.log("success");
var id = parseInt(data);
alert(id);
});
return false;
});
alert(id) returns NaN.
add.php:
require('inc/connect.php');
$quote = $_POST['quote'];
$quotes = mysql_real_escape_string($quote);
//echo $quotes . "Added to database";
mysql_query("INSERT INTO entries (quote) VALUES('$quotes')")
or die(mysql_error());
echo mysql_insert_id($quote);
I am new to JavaScript and PHP and I’m really trying to learn and understand it. How can I get the id from the submitted quote?
The error in your pastie code says:
The resource it is expecting is the link identifier returned by mysql_connect() which I don’t see in your code. Regardless, the mysql_insert_id() behavior if you DONT pass it a resource is to use the last one opened.
Changing this line:
to this:
should take care of that error. As far as the overall issue, I’m concerned because I don’t see where you are connecting to your database at all.