0

I am trying to get a form to work, but when I call ti with ajax, it will not work.

// ----------------------------EDIT----------------------------

I actually found exactly what I was looking for while browsing around.

jQuery Ajax POST example with PHP

I just have one question, would this be the best way to get the data, or could I call it from an array somehow?

post.php

$errors = array(); //Store errors
$form_data = array();
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$_POST['name'])); //Get data

if (!empty($errors)) {
    $form_data['success'] = false;
    $form_data['errors']  = $errors;
} else {
    $form_data['success'] = true;
    $form_data['country'] = $query['country'];//Have a bunch of these to get the data.
    $form_data['city'] = $query['city'];//Or is there an easier way with an array?
    $form_data['zip'] = $query['zip'];
    // Etc, etc
}
echo json_encode($form_data);

Then in index.php just call it via:

$('.success').fadeIn(100).append(data.whatever-i-have-in-post);

// ----------------------------v-ORIGINAL-v----------------------------

This is I have so far. At the bottom you can see I have an if statement to check if I could get the results from post, but it always results in "unable to get country" (I'm checking with google.com). I don't know if I am doing it correct or not. Any ideas?

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" >
    $(function() {
        $(".submit").click(function() {
            var name = $("#name").val();
            var dataString = 'name=' + name;
            if (name == '') {
                $('.error').fadeOut(200).show();
            } else {
                $.ajax({
                    type: "POST",
                    url: "post.php",
                    data: dataString
                });
            }
            return false;
        });
    });
</script>
<form id="form" method="post" name="form" style="text-align: center;">
    <input id="name" name="name" type="text">
    <input class="submit" type="submit" value="Submit">
    <span class="error" style="display:none">Input Empty</span>
    <?php
    include_once('post.php');
    if($query && $query['status'] == 'success') {
        $query['country'];
    } else {
        echo 'Unable to get country';
    }
    ?>
</form>

Post.php

$ip = $_POST['name'];
//$ip = isset($_POST['name']); // I dont know if this makes a difference
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
Community
  • 1
  • 1
12shadow12
  • 307
  • 2
  • 10
  • You are doing it wrong, if you are not submitting form by reloading page, than how will your include load the new post.php file? – Muhammad Bilal Jan 22 '15 at 07:18

1 Answers1

0

Try with this after changing the dataString = {name: name}

$(".submit").click(function() {
        var name = $("#name").val();
        var dataString = {name: name};
        if (name == '') {
            $('.error').fadeOut(200).show();
        } else {
            $.ajax({
                type: "POST",
                url: "post.php",
                data: dataString, 
                success: function(response) {
                   // Grab response from post.php
                }
            });
        }
        return false;
    });

The best way i like to grab the JSON data from ajax request. You can do it by slightly changes in your script.

PHP File

$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
echo json_encode(array('status'=>true, 'result'=>$query)); // convert in JSON Data


$(".submit").click(function() {
        var name = $("#name").val();
        var dataString = {name: name};
        if (name == '') {
            $('.error').fadeOut(200).show();
        } else {
            $.ajax({
                type: "POST",
                url: "post.php",
                data: dataString, 
                dataType: 'json', // Define DataType
                success: function(response) {

                   if( response.status === true ) {
                      // Grab Country
                      // response.data.country 
                      // And disply anywhere with JQuery
                   }
                }
            });
        }
        return false;
    });
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65