dongyi2159 2014-01-10 09:41
浏览 52
已采纳

Jquery调用PHP进程?

I'm trying to compare the value of 2 textareas, when you click the compare button the output will be displayed in the 3 text area. So what I want to happen is there are two inputs when the button is click thePHP file compares them and returns the result to the 3rd text area. How can I make this work? here's my code below and here's the jsfiddle : http://jsfiddle.net/jVxhp/

index page

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

Corrections.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>';    
}
  • 写回答

2条回答 默认 最新

  • duanjia1870 2014-01-10 09:52
    关注

    Your AJAX call is incorrect.

    The data parameter is what your php file printed. and you also can't write $_POST on the success, that's a JS code, not PHP.

    Your AJAX call should look like this:

    $('#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;
                }
            });
        });
    

    and the PHP code should look like this:

    <?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
    

    var1 and var2 is how you called your parameters on the AJAX call

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

    NOTICE: I didn'd check the Compare.php file, because I assumed your comparing engine is fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集