doumu6941 2019-06-06 20:23
浏览 205

jQuery在Data和编辑事件中传递多个值

I am creating a php file to alter file systems and I want to make this php run in the background on a ubuntu server. The html creates a webpage but the php does not trigger at all.

I followed a youtube video to this point but I need to pass both new and old string to the php in the Data part of my query which I am unsure how to do.

HTML code

        <html>


    <head>
        <meta charset ='utf-8'/>
        <title> JQuery test </title>
        <script src= "https://ajax.googleapis.cpm/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>


    <body>
    <td>Please type in the old string</td>
    <input id= 'Old_String' />
    <td>Please type in the new string</td>
    <input id= 'New_String' />
            <script>
                $(document).ready(function() {
                    $('#New_String').change(function(){  
                    $.ajax({
                        type: 'GET',
                        url: 'replace.php',
                        data: 'Old_String='+$('#Old_String').val(),
                         success : function(msg){
                            $('#results').html(msg);
                         }
                            })

                    });
                });
            </script>

            <table border="0">


            <div id="results' ></div>  

    </body>

My php code

<?php
$valid = true;

if (!empty($_GET["Old_string"])) {
    $Old_string = $_GET["Old_string"];
} else {
    $valid = false;
    echo "Old string is Empty . Please Enter value to proceed
";
}
if (!empty($_GET["New_string"])) {
    $New_string = $_GET["New_string"];
} else {
    $valid = false;
    echo "New string is Empty . Please Enter value to proceed
";
}
if ($valid) {
    echo "all input correct
";
    $myfile = fopen("word.txt", "r+") or die("Unable to open file!");
    if (flock($myfile, LOCK_EX)) {
        $text = fread($myfile, filesize("word.txt"));
        $count = 0;
        $newstring = str_ireplace($Old_string, $New_string, $text, $count);
        file_put_contents("word.txt", $newstring);
        echo "Number of changes made = " . $count;
        flock($myfile, LOCK_UN); // unlock the file
    } else {
        // flock() returned false, no lock obtained
        print "Could not lock $filename!
";
    }
    fclose($myfile);
}
?>
}

For some reason my PHP does not fire at all and no output is shown in the div results. Am I passing the values incorrectly or am I not quite doing this right? Please any suggestion would be appreciated. I am also trying to switch the event so that it triggers on a button click if you could show me how to do that I would very much appreciate it.

  • 写回答

2条回答 默认 最新

  • douzui0143 2019-06-06 20:34
    关注

    You're referencing $_GET["Old_string"] in your PHP code but passing it by query string parameter under the Old_String key in this line.

    data: 'Old_String='+$('#Old_String').val(),
    

    use case consistently, either Old_string or Old_String in both places.

    To pass both values, I'd use a simple json object instead of building a query string manually.

    data: { 'Old_String': $('Old_String').val(), 'New_String': $('New_String').val() }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料