user3650099
user3650099

Reputation: 131

Getting undefined index when using ajax.

I am trying to echo data from php to jquery and the problem is that somehow jquery is not passing the data to php. The var_dump of the variable $url echo's string'' or NULL. Expected output is to echo what the user had typed on the input field on screen where the id="domain-hits" is.

HTML

<div id="domain-hits"></div>
<input onblur="checkPR()" type="text" class="input_text_metas_submit" name="url" value="http://" id="urlpr" />

Jquery / AJAX

$(function () {
    jq2('#urlpr').on('blur', function (e) {
        $.ajax({
            type: 'post',
            url: 'onlydomain.php',
            data: $('#urlpr').serialize(),
            success: function (data) {
                $("#domain-hits").html(data);
            }
        });

        e.preventDefault();
    });
});

PHP - onlydomain.php

<?php
  $url = isset($_GET['url']) ? $_GET['url'] : '';
  var_dump($url); // it echo's string ''.
?>

I am a noob in ajax please help me on this, it's much appreciated. Thanks.

Upvotes: 0

Views: 81

Answers (1)

John Conde
John Conde

Reputation: 219804

You're sending the form data via POST by trying to retrieve it via GET. Change:

$url = isset($_GET['url']) ? $_GET['url'] : '';

to:

$url = isset($_POST['url']) ? $_POST['url'] : '';

Upvotes: 4

Related Questions