将数组作为一个变量从JavaScript发送到PHP
我有一个表单,URL是这样的:将数组作为一个变量从JavaScript发送到PHP,php,javascript,jquery,submit,form-submit,Php,Javascript,Jquery,Submit,Form Submit,我有一个表单,URL是这样的: http://hostname/projectname/classname/methodname/variablename var currentrelations = new Array(); $(".ioAddRelation").each(function(index) { currentrelations[index] = $(this).html(); }); 在我的JavaScript中,我填充如下数组: http:
http://hostname/projectname/classname/methodname/variablename
var currentrelations = new Array();
$(".ioAddRelation").each(function(index) {
currentrelations[index] = $(this).html();
});
在我的JavaScript中,我填充如下数组:
http://hostname/projectname/classname/methodname/variablename
var currentrelations = new Array();
$(".ioAddRelation").each(function(index) {
currentrelations[index] = $(this).html();
});
这个数组有两个值['eeeeee','eeeeee']
因此,url是:
http://localhost/Mar7ba/InformationObject/addIO/eeeeee,eeeeee
在我的PHP中,在InformationObject类中,在方法addIO上:
public function addIO($currentRelations =null) {
$name = $_POST['name'];
$type = $_POST['type'];
$concept = $_POST['concept'];
$contents = $_POST['contents'];
$this->model->addIO($name, $type, $concept, $contents);
if (isset($_POST['otherIOs'])) {
$otherIOs = $_POST['otherIOs'];
$this->model->addOtherIOs($name, $otherIOs);
}
$NumArguments = func_num_args();
if ($currentRelations!=null) {
$IOs = $_POST['concetedIOs'];
$this->model->setIoRelations($name,$IOs, $currentRelations);
}
exit;
include_once 'Successful.php';
$s = new Successful();
$s->index("you add the io good");
}
但是当我使用以下语句打印数组$currentRelations
时:
echo count($currentRelations)
结果是1而不是2
,当我使用该语句echo$currentRelations[0]
打印第一个元素时,我得到e not eeeeee
为什么呢?解决办法是什么?我做错了什么?正如我所评论的,
$currentRelations
是一个字符串,因此对任何不是数组或对象的类型使用count
将返回1。另外,请注意,当您对字符串执行此操作时,
$currentRelations[0]
,您正在访问该字符串从零开始的索引中的字符。由于字符串是字符数组,因此可以使用方括号访问字符串中的特定字符。这就是为什么echo$currentRelations[0]代码>在您的代码中打印e
。
要拆分字符串,应使用如下函数进行分解:
然后看看你得到了什么
var_dump($curRel);
希望有帮助。为什么不使用JSON.parse
?我如何使用它?我不知道。在JavaScript中,您使用将数组转换为字符串并将其发送到服务器。在PHP中,您使用将字符串转换回数组。您的意思是在JS中我必须这样做吗$currentRelations=json\u encode($currentRelations)
?似乎$currentRelations
就是字符串eeeeee,eeeeee
。Try$curRel=explode(“,”,$currentRelations)代码>然后<代码>计数($curRel)代码>。