Php 如何在jquery中使用$post访问?

Php 如何在jquery中使用$post访问?,php,ajax,select,post,options,Php,Ajax,Select,Post,Options,我的php代码: <select><option>your gender</option></select> <script type="text/javascript"> $('input:radio').change(function(){ var value = $("div input[type='radio']:checked").val(); if(value == "female"){ $("#item").ht

我的php代码:

<select><option>your gender</option></select>
<script type="text/javascript">
$('input:radio').change(function(){

var value = $("div input[type='radio']:checked").val();
if(value == "female"){
    $("#item").html("<div id='items'><select style=\"float:left;margin-bottom:2px;\" name='services[]'><?php $x='<span class=\"price\">'; $y='</span>';  $sql = 'SELECT * FROM service_female'; $res =mysql_query($sql);if($res===false) {echo mysql_error();}
    while($row = mysql_fetch_array($res))
        {
         $service_price=$row['service_price']; ?>
         <option><?php echo $row['service_name'].' - '.$service_price; ?></option><?php }?></select></div><input type='button' id='add' class='img'   alt=' ' value='+''>");`
$fname、$lname、$mobile-print,但$service-Undefined索引:services。帮助我发布所选选项值的值在php中是一个服务器端变量。使用name='services[]无法从客户端javascript访问它。我不知道PHP有多好,但据我所知,您的$\u POST变量得到一个数组:

<?php 
if(isset($_POST['add'])){
    if(!empty($_POST['sex']) and !empty($_POST['fname']) and !empty($_POST['lname'])){
        $gender = $_POST['sex'];
        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $mobile = $_POST['Mnumber'];
        $services = $_POST['services'];
    }
}
?>

您必须以创建html输出的方式创建javascript变量

if(isset($_POST['services'])) {
   echo $_POST['services'][0];
}

请确保$\u POST参数中没有任何敏感数据,因为此代码将把POST中的所有值打印到页面源中。

如果我想使用jquery动态添加选择值,还有什么方法?这应该是一条注释,而不是answertry console.log\u POST;相反,请检查控制台中的输出修改答案,请再试一次。如果您将页面的html源代码粘贴到某个位置,请发布从浏览器undefined和php undefined索引呈现的最终输出
<script language="JavaScript">
var _POST = JSON.parse('<?php echo json_encode($_POST);?>');
alert(_POST['fname']);
</script>