将HTML转换为php数组
Convert HTML to php array
我目前有一个表格里面。我想把这些值传递给php脚本。我最好的办法是什么?我查过的所有资料都不适用。
我的表单是这样设置的:
<form id="pageform" action="phpscript.php" method="post">
<table>
<tbody>
<tr>
<td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
</tr>
<tr>
<td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
</tr>
<tr>
<td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
</tr>
</tbody>
</table>
<input type="button" name="btnSubmit" id="btnSubmit" value="Submit">
</form>
jQuery: $("#btnSubmit").click(function(){
var array = [];
$(".nestedInput").each(function(n){
array[n] = $(this).val();
});
document.forms["pageform"].submit();
});
我的PHP:
<?php
$array=json_decode($_POST['json']);
print_r($array);
?>
我想做的是使用每个tr中每个输入的值运行mysqli插入。关于我如何做到这一点的任何想法?
在phpscript.php中,您可以这样访问这些变量:
$name = $_GET['txtName']
$location = $_GET['txtLocation']
$age = $_GET['txtAge']
通过表单提交的变量将存储在全局数组$_GET或$_POST中(取决于您的表单是使用GET还是POST)。您的表单没有method
属性来定义它,所以它默认为GET。
同样值得注意的是,你的提交按钮的id属性应该是id="btnSubmit"
,而不是id="#btnSubmit"
。
使用jQuery ajax方法;
function send_form(this) {
jQuery.ajax({
type: 'POST',
url: $(this).attr('action'),
data: $(this).serialize(),
error:function(){ console.log('error'); },
success: function(data) { $console.log(data);}
});
return false;
}
表单;
<form id="pageform" onsubmit="return send_form(this);" action="phpscript.php">
<table>
<tbody>
<tr>
<td><input type="text" class="nestedInput"name="txtName" value="John"></td>
</tr>
<tr>
<td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td>
</tr>
<tr>
<td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
</tr>
</tbody>
</table>
phpscript.php;
$name = $_POST['txtName'];
$txtLocation= $_POST['txtLocation'];
$txtAge= $_POST['txtAge'];
不需要jQuery和所有这些,只需使用HTML <form>
标签的默认功能
首先,修改
<input type="button" name="btnSubmit" id="#btnSubmit" value="Submit">
<input type="submit" name="btnSubmit" id="#btnSubmit" value="Submit">
然后为你的表单标签添加一个方法,比如
<form id="pageform" action="phpscript.php" method="post">
现在,在您的phpscript.php
页面中,将var_dump($_POST)
添加到php标签的开始部分。
点击提交按钮后,var_dump()
将打印一个数组,其中包含所有传递的表单数据。
要检索php页面中的值,您可以这样做
echo $_POST['txtName'];
其中txtName
是输入的名称。
其他输入也是如此…
相关文章:
- 调用php数组中的JS函数
- 将PHP变量分配给PHP数组
- php数组转换为javascript
- 如何用javascript打印php数组
- 将PHP数组传递给js变量
- javascript变量到php数组
- 如何将JSON编码的PHP数组发送到同一页面上的数据表
- 如何在PHP数组中加载下一个youtube
- 使用Angular.js访问php数组数据
- 将Javascript数组转换为PHP数组
- 使用mysql为JSON php数组分配多个列值
- 如何通过AJAX发送PHP数组
- 使用ajax将php数组传递给javascript
- 在Javascript代码中显示PHP数组变量
- 将php数组转换为javascript数组
- 将 PHP 数组传递给 HTML 页面
- 如何将php数组转换为javascript数组
- PHP 数组到 JavaScript 数组
- 将 JavaScript 数组转换为 PHP 数组
- 将PHP数组发送到Javascript