Jquery调用PHP进行进程



我正在尝试比较 2 个文本区域的值,当您单击比较按钮时,输出将显示在 3 个文本区域中。因此,我想发生的是,单击按钮时有两个输入,PHP文件将它们进行比较并将结果返回到第三个文本区域。我怎样才能做到这一点?这是我下面的代码,这是JSFIDDLE:http://jsfiddle.net/jVxhp/

索引页

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
        $('#Compare').click(function(e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'Corrections.php',
                data: {var1:address, var2:res},
                success: function(data)
                {
                    $var1 = $_POST['var1'];
                    $var2 = $_POST['var2'];
                    document.getElementById('address').value;
                    document.getElementById('res').value;
                    document.getElementById('cor').value='Corrections.php';
                }
            });
        });
    });
</script>
<textarea type="text" id="address" 
          onfocus="if(this.value===this.defaultValue)this.value=''" onblur="if(this.value==='')this.value=this.defaultValue" 
          >Input1</textarea>

<textarea type="text" id="res" onfocus="if(this.value===this.defaultValue)this.value=''" onblur="if(this.value==='')this.value=this.defaultValue" 
          >Input2</textarea>
<textarea type="text" id="cor" style="color: silver" disabled>
Output</textarea>
<div id="btn1"><input type="button" value="Compare" onClick="valbtn()"></input></div>

更正.php

<?php
$str1 = address;//address is textarea1 that holds one of the values to be compared
$str2 = res;//res is textarea2 were textarea1 will be compared
$tempArr;
$var2;
$ctr=0;
echo "Input:<br> $str1 <br><br>Output:<br> $str2";
$strarr = (explode(" ",$str1));
echo("<br>");
$strarr2 = (explode(" ",$str2));
echo("<br>");
if(sizeof($strarr) > sizeof($strarr2)){
    $result = array_diff($strarr,$strarr2);
}else{
    $result = array_diff($strarr2,$strarr);
}
if (count($result) > 0){
    echo "Added:<br> | " ;
    foreach ($result AS $result){
        echo $result." | ";
    }
 }
echo "<br>";
for($i=0;$i<count($strarr);$i++) {
    if (strcasecmp($strarr[$i], $strarr2[$i]) != 0)
    {
      $var[$ctr] = strcasecmp($strarr[$i], $strarr2[$i]);
      $var2[$ctr] = $i;
      $ctr +=1;
    }    
}
$tempArr = preg_grep('/[0-9]/',$var);
echo"<br>Changes: <br>";
for($i=0;$i<count($var);$i++) {

      echo $strarr[$var2[$i]] . ' is changed to ' . $strarr2[$var2[$i]] . '<br>';    
}

您的 AJAX 调用不正确。

data参数是你的 php 文件打印的内容。 而且你也不能写$_POST成功,那是JS代码,而不是PHP。

您的 AJAX 调用应如下所示:

$('#Compare').click(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'Corrections.php',
            data: {var1:$('#address').val(), var2:$('#res').val()},
            success: function(data)
            {
                document.getElementById('cor').value=data;
            }
        });
    });

PHP 代码应如下所示:

<?php
$str1 = $_POST['var1'];//address is textarea1 that holds one of the values to be compared
$str2 = $_POST['var2'];//res is textarea2 were textarea1 will be compared

var1var2是在 AJAX 调用中调用参数的方式

data: {var1:$('#address').val(), var2:$('#res').val()},

注意:我没有检查 Compare.php 文件,因为我认为您的比较引擎没问题。

修改脚本:

    $('#Compare').click(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'Corrections.php',
            data: {var1: $('#address').val(), var2:$('#res').val()},
            success: function(data)
            {
                $('#cor').val(data);
            }
        });
    });

并修改你的 php:

<?php
$str1 = $_POST['var1'];//address is textarea1 that holds one of the values to be compared
$str2 = $_POST['var2'];//res is textarea2 were textarea1 will be compared

相关内容

  • 没有找到相关文章

最新更新