javascript - 将数据从 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 .
我怎样才能做到这一点?
解决方案
另一种方法是使用 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!!';
}
推荐阅读
- python - python中的后台无限循环
- php - 如何将 jSignature 与 php 集成
- javascript - 如何继承这个javascript函数?
- scala - 链接 Scala 嵌套期货
- reactjs - 如何在每次单击时更改状态?
- python - 使用 Pycharm“在 python 控制台中运行”时如何进行相对导入?
- java - 无法替换阿拉伯字符
- c++ - 如何将整数转换为常量 void 指针?
- android - 为下载/文档文件夹中的文件类型/扩展注册 Android 应用程序
- python - Python 3 numpy 对矩阵使用整数除法,对向量使用正则除法?