0

Here is my JS code:

   function info(){
    $.ajax({
        type:'POST',
        url:'doSomething.php',
        data:'text='+encodeURIComponent($('.text').html())
            +'&info1='+encodeURIComponent($('.info1').html())
            +'&info2='+encodeURIComponent($('.info2').html())
            +'&info3='+encodeURIComponent($('.info3').html())
            +'&var1='+$('#var1').val()
            +'&var2='+$('#var2').val()
            +'&var3='+$('#var3').val(),
        success:function(){$('.action').fadeIn(500).delay(1000).fadeOut(500)}
    });
    //alert("info - did something, great!");
    return false;
    };

Everything works and is passed to doSomething.php script; except for:

  • var1 var2 var3

    can any one please tell me why? Is there a problem with above JS, or my PHP:

I'm new to AJAX. - thanks for helping!

Community
  • 1
  • 1
Ash501
  • 321
  • 2
  • 4
  • 10

2 Answers2

0

Do this:

 data:{prop11:'some value',prop2:'someVal2'}
TGH
  • 38,769
  • 12
  • 102
  • 135
  • I need the values to come from a PHP script that calls this JS function. $var1 $var2 $var3 are echoed in the script..What do you mean? - sorry if it's obvious... – Ash501 Jul 19 '13 at 03:27
  • thanks for answer, but can you specify a little? How can you get 'some value' from a PHP variable? - thanks. – Ash501 Jul 21 '13 at 13:21
0

I am not sure how you want to use the $var1 $var2 $var3 variables, but it seems you want to use it this way:

  function info(){
    $.ajax({
        type:'POST',
        url:'doSomething.php',
        data:'text='+encodeURIComponent($('.text').html())
            +'&info1='+encodeURIComponent($('.info1').html())
            +'&info2='+encodeURIComponent($('.info2').html())
            +'&info3='+encodeURIComponent($('.info3').html())
            +'&var1='+$var1
            +'&var2='+$var2
            +'&var3='+$var3,
        success:function(){$('.action').fadeIn(500).delay(1000).fadeOut(500)}
    });
    //alert("info - did something, great!");
    return false;
    };

Here is a small explanation:

  • PHP is server side language.

It gets the request from the user and generate the html and js files to return. If there a few usages of a variable named $var1 used inside a html/js file, they will replaced with with its value.

The client knows nothing about it, since he get the generated html/js file. At the client, the generated javascript will run with constant values generated by the server.

Aviran Cohen
  • 5,581
  • 4
  • 48
  • 75
  • so you mean at client side $var1... will be replaced with same PHP value given to client in rest of html? even $var1... inside the JS – Ash501 Jul 22 '13 at 22:54
  • 1
    This is the concept, since I am not coming from PHP background I am not sure about the syntax. From a quick search in the internet I think this syntax will do: Let me know if it works – Aviran Cohen Jul 23 '13 at 09:26
  • It works if the JS is created/parsed by PHP at server side, meaning, if the JS is inside a PHP script it works but if i include a JS file into a PHP script the server 'parser' ignores the PHP tags. - thanks! look here: http://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript – Ash501 Jul 24 '13 at 17:21
  • interesting...this could be very handy...thinking of switching JS file extensions into PHP, might make JS files unreadable to prying eyes outside of 'parsed' html scripts...got to run some tests.... – Ash501 Jul 24 '13 at 17:23
  • about hiding JS look here: http://stackoverflow.com/questions/2288790/how-to-make-js-code-invisible – Ash501 Jul 24 '13 at 17:32