如何动态地通过选择复选框值为Ajax调用变量张贴到PHP变量动态 [英] How to pass the select check box values dynamically to the ajax call variables post to php variables dynamically

查看:97
本文介绍了如何动态地通过选择复选框值为Ajax调用变量张贴到PHP变量动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了很好的选择复选框从埃里克·哈内斯...网站:

http://www.erichynds.com/examples/jquery -ui,多选小部件/演示/

我要传递从选择选项中选择HTML标签中选择的值...

 <!DOCTYPE HTML>
< HTML LANG =EN>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/>
<冠军> jQuery的多选的Widget演示< /标题>
<链接相对=样式类型=文/ CSS的href =../ jquery.multiselect.css/>
<链接相对=样式类型=文/ CSS的href =财产/ style.css文件/>
<链接相对=样式类型=文/ CSS的href =资产/ prettify.css/>
<链接相对=样式类型=文/ CSS的href =htt​​p://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css/&GT ;
<脚本类型=文/ JavaScript的SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =htt​​p://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =资产/ prettify.js>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =../的src / jquery.multiselect.js>< / SCRIPT>

<脚本类型=文/ JavaScript的>


$(函数(){

    $(选择),多选()。

});

功能select_val(){

VAR海峡='';

        $('选择[NAME =例如,基本]:选择)。每个(函数(一,选择){
            警报(海峡+ ='变种'+第(i + 1)+:+ $(选择).VAL()+,);
        });

                    $(#取)的负载(select.php,STR,函数(){})。

            }


< / SCRIPT>

< /头>
<身体ID =测试>

&其中p为H.;
    <选择标题=基本例如多个=多​​NAME =例如,基本的大小=5>
    <期权价值=选项1>选择1< /选项>
    <期权价值=选项2>选项2'; /选项>
    <期权价值=2选项>选项3'; /选项>
    <期权价值=3选项>选项4℃/选项>
    <期权价值=option5>选项5℃/选项>
    <期权价值=option6>选项6≤/选项>
    <期权价值=option7>选7 LT; /选项>
    <期权价值=option8>选项8示/选项>
    <期权价值=option9>选项9 LT; /选项>
    <期权价值=option10>选项10 LT; /选项>
    <期权价值=option11>选项11 LT; /选项>
    <期权价值=option12>选项12 LT; /选项>
    < /选择>
&所述; / P>
<按钮ID =select_val的onclick =select_val()>提交< /按钮>
< D​​IV ID =取与GT;< / DIV>
< /身体GT;
< / HTML>
 

@Malik:我已经加入了code与您的JavaScript,而如果我选择2复选框比数值显示像这样在第一次报警1 VAR1:选项1,,并在第二警报 VAR1:选项,VAR2:选项,

在该VAR1以上:选项,并VAR1:选项,VAR2:选项,再次逗号重复过去的价值,请检查也只发送一个值,但不重复的值,如果发送比第二后第一个值值必须发送到发送的第二件托运......请......

..但不是拳头,第二个值

但是在PHP脚本它不附和请检查...

PHP脚本:

 < PHP

$ VAR1 = $ _ POST ['VAR1'];
$ VAR2 = $ _ POST ['VAR2'];

回声$ VAR1;
回声$ VAR2;

?>
 

我上面的code ..here选择选项是通过复选框选中,现在我已经通过Ajax调用传递选定变量的PHP变量和回声在PHP脚本..

有关,例如:如果我选择方案1和方案2,我应该得到的选项1警觉,并再次第二警报选项2 ..

一个更多的帮助......现在这个数值已经传递给Ajax调用,并在那里的AJAX变量必须是动态的...我使用的负载()函数AJAX ..

  $(#取)的负载。(select.php,{VAR1:VAR1,VAR2:VAR2 ......},功能(){}) ;
 

下面如何动态的Ajax调用传递复选框变量。

 < PHP

$ VAR1 = $ _ POST ['VAR1'];
$ VAR2 = $ _ POST ['VAR2'];
。
。
。


回声$ VAR1;
回声$ VAR2;

?>
 

解决方案

好了,我们开始吧。从我的理解,你有一个多选,并希望推动的是一个数组中选择的所有这些值,并使用AJAX发送到PHP文件。这就是我想出了:

  $('[NAME =提交])。点击(函数(五){
    即preventDefault();
    变种阵列= [];
    $('选择:选择)。每个(函数(I,价值){
        数组[我] = $(本).VAL();
    });
    //这里让你的AJAX调用到PHP文件
    $阿贾克斯({
        键入:POST,
        网址:路径/到/为file.php,
        数据:{selected_values​​:数组,otherVal:'别的东西'}
    });
});
 

捣鼓多选

在你的PHP页面,使用方法:

  $阵列= $ _ POST ['selected_values​​'];
的foreach($数组作为$关键=> $值){
    //做一些与键/值。
}
 

希望它可以帮助你。

I got nice select check box from eric hanes ...site :

http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/

I have to pass the values that are selected from the select option in the select html tags...

    <!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery MultiSelect Widget Demo</title>
<link rel="stylesheet" type="text/css" href="../jquery.multiselect.css" />
<link rel="stylesheet" type="text/css" href="assets/style.css" />
<link rel="stylesheet" type="text/css" href="assets/prettify.css" />
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="assets/prettify.js"></script>
<script type="text/javascript" src="../src/jquery.multiselect.js"></script>

<script type="text/javascript">


$(function(){

    $("select").multiselect();

});

function select_val(){

var str = '';

        $('select[name="example-basic"] :selected').each(function(i, selected){ 
            alert(str += 'var'+(i+1) +':'+ $(selected).val()+','); 
        });

                    $("#fetch").load("select.php",str,function(){});

            }


</script>

</head>
<body id="test">

<p>
    <select title="Basic example" multiple="multiple" name="example-basic" size="5">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
    <option value="option4">Option 4</option>
    <option value="option5">Option 5</option>
    <option value="option6">Option 6</option>
    <option value="option7">Option 7</option>
    <option value="option8">Option 8</option>
    <option value="option9">Option 9</option>
    <option value="option10">Option 10</option>
    <option value="option11">Option 11</option>
    <option value="option12">Option 12</option>
    </select>
</p>
<button id="select_val" onclick="select_val()"> Submit </button>
<div id="fetch"></div>
</body>
</html>

@Malik : I have added the code with your javascript, and if I select 2 check boxes than the values are displaying like this in first alert1 var1:option1, and in the second alert var1:option1,var2:option2,

In the above the var1:option1, and var1:option1,var2:option2, again the comma is repeating for the last value please check and also sending only one value but not repeated value if the 1st value sent than after the second value has to sent ..but not fist and second value to sent for the second checked...please check...

but in the PHP script it is not echoing please check...

php script :

<?php 

$var1= $_POST['var1'];
$var2 = $_POST['var2'];

echo $var1;
echo $var2;

?>

I have the above code ..here the select options are selected through check box and now i have to pass the selected variables to the php variables through the ajax call and echo in the php script..

For eg : if I select option 1 and option 2, I should get alert of option 1 and again second alert option 2..

One more help ...Now this values has to pass to the ajax call and there the ajax variables has to be dynamic... I am using the load() ajax function ..

$("#fetch").load("select.php",{var1:var1,var2:var2......},function(){});

here how to pass the check box variables dynamically in the ajax call.

<?php 

$var1= $_POST['var1'];
$var2 = $_POST['var2'];
.
.
.


echo $var1;
echo $var2;

?>

解决方案

Okay, here we go. From what i understand you have a multiselect, and want to push all those values that are selected in an array, and send it to a PHP file using ajax. Here's what i came up with:

$('[name=submit]').click(function(e){
    e.preventDefault();
    var array = [];
    $('select :selected').each(function(i,value){
        array[i] = $(this).val();
    });
    //here make your ajax call to a php file
    $.ajax({
        type: "POST",
        url: "path/to/file.php",
        data: { selected_values: array, otherVal: 'something else' }
    });
});

FIDDLE FOR MULTI SELECT

In your PHP page, use:

$array = $_POST['selected_values'];
foreach($array as $key => $value){
    //do something with the key/value.
}

Hope it helps you.

这篇关于如何动态地通过选择复选框值为Ajax调用变量张贴到PHP变量动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆