calling js from php and returning value to php from js

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    calling js from php and returning value to php from js

    hi there i am making an application in which i have a drop down menu which onchange passes a value(this.valu e) to a js function which gets the value and
    calls an ajax routine and passes that value to a php page and then takes back the response ,i have done it all but now i was trying to encrypt the information or the string or the value that is passed through the drop down menu and concatinated with the url and passed to that page and then on that page it would call the js function in php and pass that encoded string to an another decoding Javascript function and takes back the decoded string then run the query for the desired values and then response back but i am not doing it right i am pasting only the relevent js and php code here ,thanks for any help in advance.
    function Pupu(val) is called onchange and passes the value to the getflds.php page,and function xor_str(url) id the encoding function that encodes the string
    MAIN PAGE.PHP
    [code=javascript]
    function xor_str(url){
    //var to_enc = document.forms['the_form'].elements["str"].value;
    var to_enc = url;
    //var xor_key=documen t.forms['the_form'].elements.xor_k ey.value
    var xor_key=6;
    var the_res="";//the result will be here
    for(i=0;i<to_en c.length;++i){
    the_res+=String .fromCharCode(x or_key^to_enc.c harCodeAt(i));
    }
    //document.forms['the_form'].elements.res.v alue=the_res;
    return the_res;
    }

    function Pupu(val){
    if(val!="" && val!="none"){
    var fobj=document.f orms['Selfrm'];
    var valar=val.split ("|");
    var cat_id=valar[0];
    var prd_id=valar[1];
    var rows=valar[2];
    //alert(prd_id);
    var prd_name=fobj.e lements['prod'+rows].options[fobj.elements['prod'+rows].selectedIndex].text;
    prd_name=stripp er(prd_name);
    var url="getflds.ph p?";
    var cnct_str="cat_i d="+cat_id+"&pr d_id="+prd_id+" &prd_name="+prd _name+"&rows="+ rows;
    var dec_str=xor_str (cnct_str);
    alert(dec_str);
    var enc_str=decrypt _str(dec_str);
    alert(enc_str);
    return;
    url=url+"str="+ dec_str;
    //alert(url);
    //return;
    xmlHttp=GetXmlH ttpObject();
    if(xmlHttp==nul l){
    alert("Please Upgrade your browser to continue");
    }else{
    xmlHttp.onready statechange=sta teFlds;
    xmlHttp.open("G ET",url,true) ;
    xmlHttp.send(nu ll);
    }
    }
    }
    function stateFlds(){
    if(xmlHttp.read yState==4){
    if(xmlHttp.stat us==200){
    alert(xmlHttp.r esponseText);
    //var a=xmlHttp.respo nseText;
    //var pipe=a.split("^ ");
    //var tab=pipe[0];
    //var rows=pipe[1];
    //alert(tab);
    //document.getEle mentById('specs '+rows).innerHT ML=tab;
    }
    }
    }

    [/code]
    PAGE:GETFLDS.PH P
    [CODE=php]
    <?
    $str=$_GET['str'];
    echo $str;
    echo "<script type='text/javascript' language='javas cript'>decrypt_ str(".$str.");</script>";
    ?>
    <script type="text/javascript" language="javas cript">
    function decrypt_str(){
    alert("hi");
    var to_dec=<?php echo $str;?>;
    var dec_str;
    document.forms['the_form'].elements.dec_r es.value="";
    var xor_key=6;
    for(i=0;i<to_de c.length;i++){
    dec_str+=String .fromCharCode(x or_key^to_dec.c harCodeAt(i));
    }
    document.write( dec_str);
    }
    </script>
    [/CODE]
    REGARDS,
    OMER ASLAM
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    What in the world are you trying to do?

    You used "and" 14 times.

    Here's what i understood (miraculously)

    1. You have a PHP program that returns a value to the page
    2. That value is passed to a js function
    3. This js function uses this variable somehow to call PHP again with AJAX.
    4. ???
    5. Profit!

    (sorry last two were digg.com jokes)

    But that's all I understood. May be bullet point or put some line breaks in your question so we can understand it.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      I got to agree with the previous post. Line brakes and periods are your friends! :)

      That said... what exactly is the problem?
      You've tried to explain what it is that you are trying to do, but I fail to see why / how it is not working.

      Comment

      • omerbutt
        Contributor
        • Nov 2006
        • 638

        #4
        Originally posted by dlite922
        What in the world are you trying to do?

        You used "and" 14 times.

        Here's what i understood (miraculously)

        1. You have a PHP program that returns a value to the page
        2. That value is passed to a js function
        3. This js function uses this variable somehow to call PHP again with AJAX.
        4. ???
        5. Profit!

        (sorry last two were digg.com jokes)

        But that's all I understood. May be bullet point or put some line breaks in your question so we can understand it.
        1st....i believe miracles do happen......tha nks for tha concentration.. .:D...and excluding the last two DIGG JOKES you are not even right in the first three points not even near to what i am trying to do but lemme elaborate it again
        NOW
        • the main thing i am tying to do is to encrypt a string in javascript
        • send it to a php page
        • and there after getting the encrypted string decode it again by calling a javascript function BUT through PHP cause i aint got any other option

        the reason is that i am sending this value through an AJAX call.It means i will not land on that particular page,so that i would click any button and call that javascript function NEITHER can i have a "<BODY onload='JAVSCRI PT FUNCTION();'> TAG "
        so that i may call that function onload .i have to do it this way .........SEQUEN TIALLY STEPS AFTER STEPS ........THROUGH OUT IN PHP



        i got to encrypt that string by calling a JS function through php and OF-COURSE
        if i send i particular string to any function to get the results i need to pass it back too......WHATS WORRY IN IT .....dont you guys do like that [:O]
        and when i got to do it i gotta do it ......if you can tell me my regards ,if you cant NO WORRIES I HAVE TO DO IT NO MATTER HOW,thanks for any help in advance
        regards,
        Omer Aslam.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Ok. That's kind of odd... but ok.

          Couldn't you just decode it after you get the result from the PHP script?
          (I assume it is meant to do something more than you showed us. Otherwise it would be completely redundant)

          If you do:
          [code=php]
          <?php
          echo "<script type=text/javascript>docu ment.write("Hi" );</script>";
          ?>
          [/code]
          And call that via AJAX, you won't just get "Hi", you will get the entire thing as a string. It won't be read as any form of markup or script, but simply as a string.

          The only realistic option is to decode the string after you get it back from the AJAX request.
          Either that or to make a PHP version of your decoding function and decode it within the PHP code before sending it.

          Comment

          Working...