简体   繁体   English

ul形式不将数据发送到$ _POST

[英]ul in form not sending data to $_POST

I have a form with ul as a dropdown menu: 我有一个形式为ul的下拉菜单:

<div class="dropdown">
    <button id="dLabel" class="form-control form-white dropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Account Type

Doctor Patient 医生病人

How do I send what I select in it to a PHP $_POST variable? 如何将选择的内容发送到PHP $_POST变量?

I tried this post didn't work. 我试过这篇文章不起作用。 Then, I got this form from this bootsrap templates signup form but I don't realy know javascript. 然后,我从bootsrap模板注册表单中获得了此表单,但是我真的不了解javascript。

Here is the full form code: 这是完整的表单代码:

<form action="account.php" class="popup-form" method="post">
    <input type="text" name="fname" class="form-control form-white" placeholder="Full Name">
    <input type="text" name="email" class="form-control form-white" placeholder="Email Address">
    <input type="text" name="username"  class="form-control form-white" placeholder="User Name">
    <input type="password" name="passwrd" class="form-control form-white" placeholder="Password">
    <div class="dropdown">
        <button id="dLabel" class="form-control form-white dropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Account Type
        </button>
        <ul class="dropdown-menu animated fadeIn" role="menu" aria-labelledby="dLabel">
            <li class="animated lightSpeedIn"><a class="cl" href="#">Doctor</a></li>
            <li class="animated lightSpeedIn"><a class="cl" href="#">Patient</a></li>
        </ul>
    </div>
    <div class="checkbox-holder text-left">
        <div class="checkbox">
            <input type="checkbox" value="None" id="squaredOne" name="check" />
            <label for="squaredOne"><span>I Agree to the <strong>Terms &amp; Conditions</strong></span></label>
        </div>
    </div>
    <input type="submit" class="btn btn-submit"></input>
</form>

Try this : 尝试这个 :

<div class="dropdown">
    <button id="dLabel" class="form-control form-white dropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Account Type
    </button>
    <ul class="dropdown-menu animated fadeIn" role="menu" aria-labelledby="dLabel">
        <li class="animated lightSpeedIn"><a class="cl" href="#">Doctor</a></li>
        <li class="animated lightSpeedIn"><a class="cl" href="#">Patient</a></li>
    </ul>
</div>

JS Code : JS代码:

$(".cl").click(function () {
     var val = $(this).html();
     $.post("test.php",{v:val},function (data){
       alert(data);
     });
 });

test.php test.php的

<?php
if(isset($_POST["v"]) && $_POST["v"] != "") {
   echo $_POST["v"];
   exit;
}
?>

I'm assuming that your HTML and PHP files are in same folder.And you've included jQuery library in your HTML. 我假设您的HTML和PHP文件位于同一文件夹中,并且您在HTML中包含了jQuery库。

<ul> and <li> are not form tags that send value, you need to use <select> instead. <ul><li>并不是发送值的表单标签,您需要使用<select>

<select name="type">
  <option value="doctor">Doctor</option>
  <option value="patient">Patient</option>
</select>

In <ul> <li> tags you cannot select a value. <ul> <li>标记中,您不能选择一个值。 If you are using any pre-defined jquery library then you can pass by post data. 如果您使用任何预定义的jquery库,则可以按发布数据传递。 Else change the ul li tags to "radio buttons or checkboxes or select" according to your requirement. 否则,根据您的要求将ul li标签更改为“单选按钮或复选框或选择”。

Using the below code i fixed the problem 使用以下代码我解决了问题

            <form action="account.php" class="popup-form" method="post">
                <input type="text" name="fname" class="form-control form-white" placeholder="Full Name">
                <input type="text" name="email" class="form-control form-white" placeholder="Email Address">
                <input type="text" name="username" class="form-control form-white" placeholder="User Name">
                <input type="password" name="passwrd" class="form-control form-white" placeholder="Password">
                <input class="span2" id="a_type" name="a_type" type="hidden">
                <div class="dropdown">
                    <button id="dLabel" class="form-control form-white dropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Account Type
                    </button>
                    <ul class="dropdown-menu animated fadeIn" role="menu" aria-labelledby="dLabel">
                        <li onclick="$('#a_type').val('Doctor'); $('#searchForm').submit()" class="animated lightSpeedIn"><a class="cl" href="#">Doctor</a></li>
                        <li onclick="$('#a_type').val('Patient'); $('#searchForm').submit()" class="animated lightSpeedIn"><a class="cl" href="#">Patient</a></li>
                    </ul>
                </div>
                <div class="checkbox-holder text-left">
                    <div class="checkbox">
                        <input type="checkbox" value="None" id="squaredOne" name="check" />
                        <label for="squaredOne"><span>I Agree to the <strong>Terms &amp; Conditions</strong></span></label>
                    </div>
                </div>
                <input type="submit" class="btn btn-submit"></input>
            </form>

In the li tag add an attribute. li标签中添加一个属性。 Example: 例:

<li class="animated lightSpeedIn" name_pressed="Doctor"><a href="#">Doctor</a></li>

and I assume you have an event for clicking the the li . 我假设您有一个单击li的事件。 If not, you can add a class. 如果没有,您可以添加一个类。 When you click that li with the respective class take the attribute. 当您单击带有相应类的li ,将带有该属性。 In this exemple you will take the "Doctor" from name_pressed attribute and with an if redirect on the page you want. 在此name_pressed您将从name_pressed属性中获取“医生”,并在想要的页面上使用if重定向。

Like this: 像这样:

<li class="animated lightSpeedIn clickable_li" name_pressed="Doctor">Doctor</li>

and the javascript: 和javascript:

$(".clickable_li").click(function (e) {
    var name = $(this).attr('name_pressed');

    if (name == 'Doctor') {
        //make something
    }
});

Or you can add onClick event. 或者,您可以添加onClick事件。 Check this 检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM