2012-07-03 97 views
-1

PHP代碼PHP POST方法使用jQuery

if(empty($_POST) === false) 
{ 
    $required_fields = array('s_group', 's_choice'); 
    foreach($_POST as $key=>$value) 
    { 

     if(empty($value) && in_array($key, $required_fields) === true) 
     { 
      $error = 'All * fields are required'; 
      break 1; 
     } 

    } 


    if(empty($error) === true) 
    { 
     echo $_POST['s_group']; 
     echo $_POST['s_choice']; 
     echo $_POST['choice1']; 
     echo $_POST['choice2']; 
    } 
} 

echo $error; 

這是HTML代碼

<form action="create_room.php" method="post" name="form1"> 
<select name="s_group"> 
    <option value=""><-- Please Select Item --></option> 
    <? 
    $strSQL = "SELECT * FROM room_group_options ORDER BY op_id"; 
    $objQuery = mysql_query($strSQL); 
    while($objResult = mysql_fetch_array($objQuery)) 
    { 
    ?> 
    <option value="<?=$objResult["op_id"];?>"><?=$objResult["group"];?></option> 
    <? 
    } 
    ?> 
    </select> 

    <script src="http://code.jquery.com/jquery-latest.js"></script> 

<p>Number of Choice : 
<select id="pagelist" name = "s_choice"> 
    <option value=""><-- Please Select Item --></option> 
    <option value="twoChoices">2 Choices</option> 
    <option value="threeChoices">3 Choices</option> 
    <option value="fourChoices">4 Choices</option> 
    <option value="fiveChoices">5 Choices</option> 
</select> 

<div id="twoChoices" style="display:none"> 

    Insert your choice<br> 
    1:<input type="text" name="choice1" /><br /> 
    2:<input type="text" name="choice2" /><br /> 

</div> 

<div id="threeChoices" style="display:none"> 

    Insert your choice<br> 
    1:<input type="text" name="choice1" /><br /> 
    2:<input type="text" name="choice2" /><br /> 
    3:<input type="text" name="choice3" /><br /> 

</div> 


    <script language="javascript"> 
    $("#pagelist").change(function() 
    { 
    var viewID = $("#pagelist option:selected").val(); 
    $("#pagelist option").each(function() 
    { 
     var hideID = $(this).val(); 
     $("#"+hideID).hide(); 
    }); 
    $("#"+viewID).show(); 
    }); 
</script> 

<input type="submit" name "submit" value="Create"> 
</form> 

我是一個PHP新手。填滿所有空白後,選擇選項並點擊提交按鈕,s_group和s_choice中的值位於$ _POST中,但choice1和choice2未到。

我怎樣才能獲得這些價值?

+0

是不是禁用了? display:none –

+0

爲什麼不只是使用:$(「#pagelist」)。val()發送!? – Yang

回答

1

改寫下列代碼
插入你的選擇

1:<input type="text" name="choice1" value = ""/><br /> 
2:<input type="text" name="choice2" value = ""/><br /> 

Insert your choice<br> 
1:<input type="text" name="choice3" value = ""/><br /> 
2:<input type="text" name="choice4" value = ""/><br /> 
3:<input type="text" name="choice5" value = ""/><br /> 

並且不要重複你的選擇名字。以這種方式製作您的JavaScript,以便在頁面列表中更改顯示/隱藏choice3,choice4和choice5按鈕。

+0

謝謝,它的工作:) –

1

只做文本輸入按鈕Choice1,choice2 ...等的一個實例,並在頁面列表更改時通過javascript隱藏/顯示單個文本輸入。你會在你的文章中得到你的變量。

+0

謝謝,它的作品:) –