-4

I've a jquery script that call the number2.php page, which is supposed to execute and show the result in a <div>. The problem is that is not working. Can you help me? Thanks.

<script>
    $(function() {
        $('#submit').click(function() {
            if ($('#taille').val() != 0) {
                var param = 'l=' + $('#taille').val();
            }
            else {
                var param = 'b=' + $('#datepicker').val() + 'c=' + $('#datepicker1').val() + 'num' + $('#num').val();
            }
            $('#retour').load('number2.php', param);
        );  
    });
</script>
Adam S
  • 16,144
  • 6
  • 54
  • 81
wiwi
  • 29
  • 1
  • 3
  • 1
    En anglais s'il vous plaît – Rory McCrossan Jul 09 '15 at 10:36
  • 1
    Translation : I've a jquery script that call the number2.php page, which is supposed to execute and show the result in a
    . The problem is that is not working. Can you help me? Thanks.
    – dlegall Jul 09 '15 at 10:39
  • @wiwi, what do you want to say by the fact that it doesn't work? Did you use the console to see if there is any javascript error or if the request was launched? fr trans : wiwi, qu'entends-tu par ne fonctionne pas? As-tu regardé la console javascript pour voir si il y'avait des erreurs et si la requête avait bien été lancé? Thanks. – dlegall Jul 09 '15 at 10:51
  • no the request is not launched i've got the same page refreched instead ! – wiwi Jul 09 '15 at 11:11
  • Note that you're missing a closing `}` on your click handler function. – Rory McCrossan Jul 09 '15 at 11:12
  • thx @RoryMcCrossan now it works but after loading number2.php my main page is refreshed and i mose the result !!! – wiwi Jul 09 '15 at 12:38
  • 1
    I've added an answer for you which should solve that problem. – Rory McCrossan Jul 09 '15 at 12:41

1 Answers1

1

Your code has a syntax error; it's missing closing } in the click handler function.

now it works but after loading number2.php my main page is refreshed and i lose the result !!!

In this case, you should hook to the submit event of the form element and call preventDefault() on the event to prevent the normal form submission. Try this:

$(function() {
    $('#myForm').submit(function(e) { // change #myForm to target the <form>
        e.preventDefault();
        var param = {};
        if ($('#taille').val() != 0) {
            param.l = $('#taille').val()
        }
        else {
            param.b = $('#datepicker').val();
            param.c = $('#datepicker1').val();
            param.num = $('#num').val();
        }
        $('#retour').load('number2.php', param);
    });  
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • No problem, glad to help. – Rory McCrossan Jul 09 '15 at 13:27
  • sorry to bother but when i test my parameters in php page i get l and num but not b nor c that's strange no !!! knowing that the code was working fine before i added jquery i mean i had the date result before – wiwi Jul 09 '15 at 13:28
  • @wiwi: Try the code now -- there was an issue with the `param` variable being out of scope, so in your call to `load`, `param` was `undefined`. It should now contain the values you need. – Cᴏʀʏ Jul 09 '15 at 13:34
  • 1
    @wiwi your querystring was missing the `&` character to delimit the key/value pairs. Try my updated version. – Rory McCrossan Jul 09 '15 at 14:30
  • @RoryMcCrossan: No problem. I might also argue incorporating `$.param({ ... })` instead of manually building a query string, but I think you have enough to get OP back on the right track. – Cᴏʀʏ Jul 09 '15 at 14:48
  • 1
    I agree, plus it's a 2 second change :) OP - I'd suggest passing the `param` as an object and let jQuery do the serialisation for you - see the latest update. – Rory McCrossan Jul 09 '15 at 14:49
  • how van i collect these parameters in the next page cause it's not working for me – wiwi Jul 27 '15 at 13:26
  • http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript, or if you're sending the params to a PHP page, you can retrieve them as you would any standard GET parameter. – Rory McCrossan Jul 27 '15 at 13:27
  • I'm using GET parameter and i've changed my jquery code a thousend times the result is allways the same i get only one parameter which is 'l' and none of the others – wiwi Jul 27 '15 at 13:38
  • That would suggest the `$('#taille').val() != 0` condition is always true. – Rory McCrossan Jul 27 '15 at 13:38
  • ifound the problem it's what you said earlier " the & missing " i just didn't get it any way thx alot @RoryMcCrossan you've been so helpful :D – wiwi Jul 28 '15 at 10:29