单选按钮的PHP更新

时间:2012-03-26 06:21:30

标签: php forms jquery-mobile input pdo

按下提交时,单选按钮(性别和级别)显示“未定义”。我正在使用jQuery Mobile框架。 type=text的其他字段工作正常。如何在这些单选按钮的后端插入值?我需要在PHP中显式设置值吗? (即if$_POST['gender'] == 'male'

以某种方式$ _POST ['gender']和$ _POST ['level']没有定义变量。只有当只有一个单选按钮时才有效。

 <form action="save.php" method="post">
        <label for="name">Name </label>
        <input id="name" type="text" name="name"/>

        <label for="city">City </label>
        <input id="city" type="text" name="city"/>

        <label for="lang_know">Language </label>
        <input id="lang_know" type="text" name="language"/>

        <div data-role="fieldcontain">
            <label for="lang_level">Level</label>
            <fieldset id="lang_level" data-role="controlgroup" data-type="horizontal">
            <label for="okay">Okay</label>
            <input type="radio" id="okay" name="level" value="okay"/>
            <label for="good">Good</label>
            <input type="radio" id="good" name="level" value="good"/>
            <label for="great">Great</label>
            <input type="radio" id="great" name="level" value="great"/>
            </fieldset>
        </div>

        <div data-role="fieldcontain">
            <label for="gender">Gender</label>
            <fieldset id="gender" data-role="controlgroup" data-type="horizontal">
            <label for="male">Male</label>
            <input type="radio" id="male" name="gender" value="male"/>
            <label for="female">Female</label>
            <input type="radio" id="female" name="gender" value="female"/>
            </fieldset>
        </div>

        <label for="age">Age </label>
        <input id="age" type="number" min=18 max=90 name="age"/>
        <input type="submit" value="Update"/>
    </form>

Save.php

<?php
    try{

    $name = $_POST['name'];
    $city = $_POST['city'];
    $language = $_POST['language'];

    $level = $_POST['level'];
    $gender = $_POST['gender'];

    $age = $_POST['age'];
    $email = "lapm@gmail.com";

    $connection = new PDO ('mysql:host=localhost;dbname=tongue', 'web', 'lapming1');
    $connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $connection -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    function save ($query, $email, $connect, $name, $city, $language, $level, $gender, $age) {
        $statement  =  $connect->prepare($query);
        $statement  -> bindParam(':email',$email, PDO::PARAM_STR, 127);
        $statement  -> bindParam(':name', $name, PDO::PARAM_STR, 127);
        $statement  -> bindParam(':city', $city, PDO::PARAM_STR, 127);
        $statement  -> bindParam(':language', $language, PDO::PARAM_STR, 127);
        $statement  -> bindParam(':level', $level, PDO::PARAM_STR, 127);
        $statement  -> bindParam(':gender', $gender, PDO::PARAM_STR, 127);
        $statement  -> bindParam(':age', $age, PDO::PARAM_STR, 127);
        $statement  -> execute();
    }

    $sql = 'UPDATE user
            SET name=:name, city=:city, language=:language, level=:level, gender=:gender, age=:age
            WHERE email=:email';
    $save = save ($sql, $email, $connection, $name, $city, $language, $level, $gender, $age);

    header ('Location:profile.php');

    $connection = null;
    } catch(PDOException $e) {
        echo $e->getMessage();
    };  
?>

4 个答案:

答案 0 :(得分:1)

这是我写的解决方法。

<fieldset data-role="controlgroup">
<legend>Gender</legend>
<input id="male" type="radio" name="male" value="male"/>
<label for="male">Male</label>
<input id="female" type="radio" name="female" value="female"/>
<label for="female">Female</label>
</fieldset>

save.php

//gender radio buttons
    $male = $_POST['male'];
    $female = $_POST['female'];

    if (isset($male)){
        $gender = 'male';
        };

    if (isset($female)){
        $gender = 'female';
        };

答案 1 :(得分:0)

要在表单元素上正确传输数据,您需要设置name属性。

<input type="text" name="blarg" value="" />

这将分别以$ _GET ['blarg']或$ _POST ['blarg']的形式传递给PHP。

您应该浏览表单元素并添加名称属性以匹配每个 ID 属性的值。

答案 2 :(得分:0)

我不确定为什么单选按钮不起作用,他们看起来对我来说。

至于年龄,您确定您的浏览器支持输入类型=“数字”吗?

在任何情况下,print_r($_POST)显示什么?

答案 3 :(得分:0)

我试试echo $gender = $_POST['gender'];

如果它正常工作,那么回应你的查询

echo  $sql = 'UPDATE user
            SET name=:name, city=:city, language=:language,
            level=:level, gender=:gender, age=:age
            WHERE email=:email';

我希望这可以帮助您获得理想的结果