Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  45] [ 1]  / answers: 1 / hits: 22383  / 11 Years ago, tue, september 3, 2013, 12:00:00

I'm trying to send an input value to a php script and have the returned value posted to a div, using ajax, but I can't seem to get this right. Any help/suggestions would be appreciated. Thanks!!



This is what I have by now, but console says: Failed to load resource: the server responded with a status of 404 (Not Found).



test1.php:



<script>
$.ajax({
type: 'POST',
url: 'test2.php',
data: {url: $('#id1').val()},
success: function (data)
{
$(document).ready(function(){$(#content).load(test2.php);});
}
});
</script>

<form name=input>
<input type=text id=id1>
<input type=submit>
</form>

<div id=content></div>


test2.php:



<?php
$string=$_POST['id1'];
require_once('connect.php');
$inf = SELECT * FROM `comments` WHERE date='$string';
$info = mysql_query($inf);
while($info2 = mysql_fetch_object($info)) {echo $info2->username.$info2->date;}
?>

More From » php

 Answers
2
<script>
$(document).ready(function() {
$('#submit').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'test2.php',
data: {id1: $('#id1').val()},
success: function(data)
{
$(#content).html(data);
}
});
});
});
</script>

<form name=input>
<input type=text id=id1>
<input type=submit id=submit>
</form>

<div id=content></div>


When you submit the ajax request, you're already submitting your content to test2.php, so you don't need to load it again. In the success function, you can append the result to the div from the callback.


[#75927] Monday, September 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ansleyk

Total Points: 42
Total Questions: 100
Total Answers: 83

Location: Angola
Member since Wed, Apr 13, 2022
2 Years ago
ansleyk questions
;