PHP代码中的Javascript变量通过ajax。 [英] Javascript variable in PHP code via ajax.

查看:87
本文介绍了PHP代码中的Javascript变量通过ajax。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想将变量t传递给php,以便我将其用于搜索。我尝试使用ajax,但我做错了。

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>





我不知道如何在php中使用变量。我想要$ str为't'的值。这是我的代码:





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;
                  }


                                  ?>



< br $> b $ b



和html:





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.

推荐答案

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

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


(document).ready(function(){
(document).ready(function() {


('#sub' ).click(function(){
('#sub').click(function() {


这篇关于PHP代码中的Javascript变量通过ajax。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆