Click here to Skip to main content
14,640,670 members
Rate this:
Please Sign up or sign in to vote.
Hello,
I want to pass the variable t into php so that i use it for the search. I tried with ajax but I am doing something wrong.
<pre lang="Javascript"><script >


                                    function get_selection() {
                                        var txt = '';
                                        if (window.getSelection) {
                                            txt = window.getSelection();
                                        } else if (document.getSelection) {
                                            txt = document.getSelection();
                                        } else if (document.selection) {
                                            txt = document.selection.createRange().text;
                                        }
                                        return txt;
                                    }

                                    $(document).dblclick(function(e) {
                                        var t = get_selection();
                                        //alert("Concordance for:"+" "+t);
                                        document.getElementById("word").innerHTML = t;
                                    });

                        var var_data = t;
                        $(document).ready(function() { 
                        $('#sub').click(function() { 
         $.ajax({ 
         url: 'phpPage.php?var_PHP_data='+var_data,
         type: 'GET', 
         data: { var_PHP_data: var_data },

          success: function(data) { 
            $('#result').html(data)
             } }); }); }); 

</script>


I dont know how to use the variable now in php.I want $str to be the value of 't'. Here is my code:

<?php


  
   if(isset($_GET["display"])){


            

    $fileName="https://users.cs.cf.ac.uk/KurtevaA/searchfile.txt";
    $lines = file($fileName);
    $str="<script>document.write[t]</script>";
    echo $str;
    foreach ($lines as $lineNumber => $line) {
        if (strpos($line, $str) !== false) {
          echo $line;
            return $line;
        }
    }
    return -1;
                  }


                                  ?>




and the html:
<input type="submit" value="Submit" id="sub"/> <p id="result"></p>


What I have tried:

Any help would be much appreciated as I am a beginner.
Posted
Updated 26-Apr-18 5:23am
Comments
enhzflep 25-Apr-18 20:24pm
   
You're adding "?var_PHP_data" to the url, so you need to check $_GET["var_PHP_data"] in the php code..
Heaven only knows how you think that $_GET["display"] will provide you with anything.

Hint: do a var_dump($_GET); in your php code to see what actually does make it through. ;)
Member 13358832 14-Nov-19 11:03am
   
I am passing "Text" and "Date" Types through to $_post in php i am suffering a error in "Date" " unidentify index" but in "Text" it is ok. Please resolve the error.

Thanks
Muhammad Naveed

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
$(document).dblclick(function(e) {
    var t = get_selection();
    //alert("Concordance for:"+" "+t);
    document.getElementById("word").innerHTML = t;
});

var var_data = t;

The var var_data = t; line executes before the dblclick event handler. It also has no access to the variable t defined in the event handler function, so it will default to "undefined".

Change the event handler code so that it updates the global variable:
var var_data = null;

$(document).dblclick(function(e) {
    var_data = get_selection();
    //alert("Concordance for:"+" "+var_data);
    document.getElementById("word").innerHTML = var_data;
});

$(document).on("click", "#sub", function() { 
    $.ajax({ 
        url: 'phpPage.php?var_PHP_data=' + encodeURIComponent(var_data),
        type: 'GET', 
        success: function(data) { 
            $('#result').html(data);
        }
    }); 
}); 

To access the variable from PHP:
<?php
if(isset($_GET["var_PHP_data"])){
    $str = $_GET["var_PHP_data"];
    $fileName="https://users.cs.cf.ac.uk/KurtevaA/searchfile.txt";
    $lines = file($fileName);
    foreach ($lines as $lineNumber => $line) {
        if (strpos($line, $str) !== false) {
            echo $line;
            return $line;
        }
    }
    
    return -1;
}
?>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100