1

Using ajax, I'm trying to display what is being typed in the text box, but it's not displaying anything at all for some reason. I know the ajax function itself got called, by using alert inside the function, and I think the real problem is actually in test2.php, but I'm not sure what I did wrong. Please take a look:

test1.php

<?php

include('ajax.php');

echo "<input type = 'text' name = 'select' onkeyup = 'ajax(\"test2.php\",\"select\",\"output\")'>";
echo "<div id = 'output'/>";

?>

test2

<?php

$select = $_POST['select'];
echo $select;

?>

ajax.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 

function ajax(url,select,id) {

      $.ajax({
           type: "POST",
           url: url,
           data: { select: $('select[name="select"]').val()},
           error: function(xhr,status,error){alert(error);},
           success:function(data) {
             document.getElementById( id ).innerHTML = data;
           }

      });

}

</script>
1
  • 1
    select[name="select"] is invalid. Shouldn't it be "input[name='select']" Commented Jun 16, 2015 at 18:38

2 Answers 2

3
function ajax(url,unused,id) {
    $.post(url,{select: $('input[name="select"]').val()})
    .error(function(e){
        alert(e);
    })
    .success(function(d){
        $("#"+id).text(d);
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

@ksealey Anyway for me to pass in the name in the parameter?
@frosty The name of the input field?
@ksealey Actually that's resolved. But I have a recent question open, if you want? stackoverflow.com/questions/30905603/…
0

The problem is here:

 data: {select: $('select[name="select"]').val()},

There is no select element. And if you meant to get the id named element, then you need to change it to:

data: {select: $('#select[name="select"]').val()},

or in your case:

data: {select: $('input[name="select"]').val()},

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.