iT邦幫忙

0

【javascript 傳值給 php ???】

我有1個html
1個php
目前可以將
select 的 value 傳給 php(但是是透過 get 的方式)
請問要怎麼做
才能把 hidden 的 value(h1,h2,h3,h4)
一起傳到 php 呢(透過 onchange,而且不要用 get)

<script>
function chg()
{
	str_n = document.getElementById('n').value;
	str_m = document.getElementById('m').value;
	if(str_n)
	{
		document.getElementById("m").disabled = true;
		window.open("select_submit.php?a="+str_n);
		document.getElementById("m").disabled = false;
		document.getElementById("n").value = "";
	}
	else if(str_m)
	{
		document.getElementById("n").disabled = true;
		window.open("select_submit.php?a="+str_m);
		document.getElementById("n").disabled = false;
		document.getElementById("m").value = "";
	}
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>

</title>

<form name="form1">
<select name="n" onchange="chg()">
<option value="n1">n1</option>
<option value="n2">n2</option>
<option value="n3">n3</option>
<option value="n4">n4</option>
</select>
<hr>
<select name="m" onchange="chg()">
<option value="m1">m1</option>
<option value="m2">m2</option>
<option value="m3">m3</option>
<option value="m4">m4</option>
</select>
<input type="hidden" name="h1" value="h1">
<input type="hidden" name="h2" value="h2">
<input type="hidden" name="h3" value="h3">
<input type="hidden" name="h4" value="h4">
<hr>
</form>








<?php
echo $_GET['a'];
?>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
5
Old Siao
iT邦研究生 1 級 ‧ 2014-12-02 21:13:35

使用jQuery的話,可接受嗎?
下面這篇,參考看看
http://www.dotblogs.com.tw/jhsiao/archive/2014/08/31/146422.aspx

看更多先前的回應...收起先前的回應...
fillano iT邦超人 1 級 ‧ 2014-12-02 22:09:54 檢舉

補充一下:

<pre class="c" name="code">var h1 = document.getElementsByName('h1')[0].value;

使用document.getElementsByName就可以取得你要的hidden元素,不過他取得的是同樣name元素的陣列,所以還需要用陣列所以把個別元素取出。

fillano iT邦超人 1 級 ‧ 2014-12-02 22:10:49 檢舉

打錯字...「陣列所以」=>「陣列索引」

andyto202 iT邦研究生 4 級 ‧ 2014-12-03 09:14:27 檢舉

請問 chiahung79 兄
var h1 = document.getElementsByName('h1')[0].value;
是加在 jqajax_select.js 裡面的那個地方呢??

fillano iT邦超人 1 級 ‧ 2014-12-03 16:47:05 檢舉

ㄝ,就跟你使用document.getElementById('n').value一樣阿,看你要在哪裡抓hidden的值,就在哪裡用囉。

0
shihchun520
iT邦新手 5 級 ‧ 2014-12-04 13:11:15

小小建議

PHP GET 使用方法與範例 - Wibibi
http://www.wibibi.com/info.php?tid=145

[HTML]
<form action="select_submit.php" method="get">
<input type="hidden" name="h1" value="h1">
<input type="hidden" name="h2" value="h2">
<input type="hidden" name="h3" value="h3">
<input type="hidden" name="h4" value="h4">
<input type="submit" value="送出表單"/>
</form>

[PHP]
<?php
echo $_GET[h1];
echo $_GET[h2];
echo $_GET[h3];
echo $_GET[h4];
?>

0
gituest
iT邦新手 5 級 ‧ 2014-12-30 21:38:04

<select name="m" onchange="chg()">
str_m = document.getElementById('m').value;

一個是取 name 一個是抓id 好像有點怪怪的

這樣比較對:
<select id="m" onchange="chg()">
str_m = document.getElementById('m').value;

我要發表回答

立即登入回答