tags:

views:

19

answers:

1

Hello all,

Here's the HTML part that may be wrong perhaps on the form statement (not sure):

<div id='data'></div>
  <form action="">
   <input type="text" name="nomeInput" value="" />
</form>

Here's the javascript part:

$(document).ready(function(){

$.post("testeBasico_1.php", {nomeInput : $('#nomeInput').val()}, function(resposta) {
  for (var x = 0, tamanhoDados = resposta.dados.length; x < tamanhoDados; x++){
     $('#data').append(resposta.dados[x]+'<br>');
  }
  //issue line
  $('#data').append('<br />'+resposta.venhoDoInput);
  }, "json");
});

Here's the php part:

$response = (object) array(
    'success' => TRUE,
    'dados'       => array("1", "2", "3"),
    'venhoDoInput' => $_POST['nomeInput']
);
echo json_encode($response);

When I try this, I get null on 'venhoDoInput' regardless the input field being filled or not.

What am I missing here? (it should be something very very basic), I'm just hoping that, by knowing that, I can better understand those evil code lines.

Thanks a lot in advance,

MEM

Note: If I dump($_POST['nomeInput'] on the server side script, I get nothing displayed... that's probably because I'm using js to display that data into the browser. And I'm not quite sure how to debug server side here... :s

+2  A: 

You are using an id selector, but the element you are trying to select does not have the id set. Add the attribute id="nomeInput" to the input.

Edit: Your code will submit the form on page load. In order to have it submit upon actual form submission, you need to wrap it a submit listener for the form.

HTML:

<div id='data'></div>
<form action="" id="myForm">
    <input type="text" name="nomeInput" value="" />
</form>

jQuery:

$(document).ready(function(){

    $('#myForm').submit(function() {
        $.post("testeBasico_1.php", {nomeInput : $('#nomeInput').val()}, function(resposta) {
            for (var x = 0, tamanhoDados = resposta.dados.length; x < tamanhoDados; x++) {
                $('#data').append(resposta.dados[x]+'<br>');
            }
            //issue line
            $('#data').append('<br />'+resposta.venhoDoInput);
        }, "json");
        return false;
    }
});
rr
thanks. Well spotted. ;) That as removed the null from showing, but I still can't see what I put inside the input element. I write on the input element, then I hit enter, hoping that the magic will happen. But I'm not sure about it... :s
MEM
Probably I need an event handler... otherwise only on refresh... but if we do refresh, we lose our value, hence, we never get nothing... (can this be an explanation?) So, what I would need is a event handler here?
MEM
You should trigger your ajax call on the submit event of your form. And don't forget to return false in the submit event handler otherwise your form will also be submited by the browser and the page will reload.
Pierre Henry
Yes, as Nick Craver pointed out, the form will simply submit whenever the page is loaded, so you will need to wrap the submission in an event handler. I will update my post with some example code.
rr
@rr - Thanks a lot. This is for a autocomplete so I will have no submit button. I will probably do this onblur... precise?
MEM
If you are trying to implement autocomplete functionality, you will need to listen for keypress events, which brings in a whole slew of complexities. I would recommend taking a look at some existing jQuery autocomplete plugins over trying to implement your own.
rr
:( I have tried that, I don't understand those plugins... to complicated... or not so well documented for my newbie requirements.I've just added the following:$(document).ready(function(){ $('#nomeInput').keypress(function(){ It seems to work. For now. :) Now the next part, should be grabbing the value typed on the input box onblur... ?
MEM
Trying to implement your own will get a bit more complicated than using a plugin, but that's my experience. Have you checked out http://www.pengoworks.com/workshop/jquery/autocomplete.htm? I found it to be fairly straightforward.I cannot give much advice on implementing your own however as I've never done it before.
rr
Well... I'm trying to adapt from here: http://www.nodstrum.com/2007/09/19/autocompleter/But the js is inline... it's not using json... it's generating the html on the "model"... etc... but, appart from that, the logic seems simple, just wondering about the fact that I will not use inline calls but, instead, js inside ones. Still, I will have a look at the provided link as well. But I will make my efforts to build this. :)
MEM