首页 > 解决方案 > 将数据从 jscript 发送到其他 php

问题描述

我正在做一个动态表,例如:

 <form method="POST" action="recieve.php">
  <table>
   <thead>
    <th>Value</th>
    <th>Action</th>
   </thead>
   <tbody>
    <tr>
     <td><input type="hidden" name="value_original[0]" id="value_original[0]" value="20000">
       <input type="text" id="value[0]" name="value[0]"></td>
     <td><button type="button" class="btn btn-info add_input_button" title="Add" onclick="addFunction()"></td>
     </tr>
    </tbody>
   </table>
  </form>
  <button id="send" type="submit" class="btn btn-success" onclick="check()">Aceptar</button>

添加和删​​除字段工作正常,给我额外的空间,以防我需要它们,但在我点击发送按钮后,我需要它来检查值是否正常,否则我需要它来请求特殊权限来发送它。

 <script>
   var value_original = [];
   var value = [];
   function check(){
    var j=0;
    var k=0;
    for(j;j<=i;j++){
     value_original[j] = document.getElementById('value_original['+j+']').value;
     value[j] = document.getElementById('value['+j+']').value;
     value_original[j]=Number(value_original[j]);
     value[j]=Number(value[j]);
     if(value[j]<value_original[j]){
      k++;
     }
    }
    if(k>0){
    //ask permission
     return false;
    }
    else{
     return true;
    }
   }
  </script>

我有另一个带有函数 add($data,$username,$password) 的 php,其中用户名和密码是使用当前正在工作的 sweetalert2 在权限弹出窗口中定义的,但我需要将该信息发送到 recieve.php .

我怎样才能做到这一点?

标签: javascriptphpjqueryajaxsweetalert2

解决方案


另一种方法是使用 Ajax:

     $.ajax({
        url: 'http://localhost/recieve.php?user=Me&pass=1234&data=whatever',
        dataType: 'json',
        async: true,
        success: function (PhpResponse)
        {              
           if (PhpResponse !== null) {                
              console.log(PhpResponse);                                         
           }
        }
     });

PHP 应该能够获取和处理此参数,如下所示:

if (isset($_GET["user"]) && isset($_GET["pass"])  && isset($_GET["data"])) {    
   $GLOBALS['response'] = 'I got the data!!';
}

推荐阅读