4

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;}
?>
2
  • Are you sure that test2.php is in destined url?? Your error should probably be related to file misplacement. check it Commented Sep 3, 2013 at 13:07
  • Also check your test1.php file. It has no meaning. When will you be calling ajax function?? Commented Sep 3, 2013 at 13:10

4 Answers 4

3
<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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. It still doesn't do anything when I press the submit button, which I believe to be the problem here.. I'm just not sure if I should add a post action pointing to test2 on it.
Add a DOM ready function around the jquery, and this is the right answer
Edited, the data: should contain your POST parameters.
@user2743057 This script is correct. Check whether you have loaded jquery file. Also you have mentioned in your question that file is not found, which directly means, you have not placed your file in right location, or you have not given your url right. Check that..
0
    $(document).on('click','#submit',function(e) {
        e.preventDefault();
        $.post('test2.php',{url: $('#id1').val()},function(data){
            $("#content").html(data);
        }
       });
    });

Comments

0

404 (Not Found) Error is for page not found. Please make sure that file test2.php is exist in same folder. Check url.

Also you can copy the URL from console and paste it in the browser URL to check the url correct or incorrect.

jQuery

<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>

HTML

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

Comments

0

You could try this:

<script>
    $('#submitBtn').on('click',function(){
        $.ajax({
            type: 'POST', 
            url: 'test2.php',      
            data: {url: $('#id1').val()},         
            success: function (data)
            {
               $("#content").html(data);
            }
        });   
        return false;
    });
</script>

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

<div id="content"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.