【问题标题】:Can you assign values to hidden fields in a form, through a php function, to be stored in a database?您可以通过 php 函数为表单中的隐藏字段分配值以存储在数据库中吗?
【发布时间】:2016-06-29 01:11:18
【问题描述】:
  1. 我创建了一个你知道的表格;文本字段、单选按钮和提交按钮
  2. 在上述表单中,我有一个 div,其中包含一个隐藏的单选按钮部分和一个在页面加载时使用内联 CSS display:none;
  3. 如果最终用户选择是,隐藏字段将使用 jquery 函数显示。如果用户选择 No 或 Not Sure,表单将保持隐藏状态或使用相同的 jquery 函数变为隐藏状态。
  4. 如果用户选择否或不确定,我想自动为隐藏字段分配值并将它们存储在数据库中。

这是我的表格:

<div id = "relation" style = "display: none;">
    <p class= "form-p" >Who are you related o?</p>                    
    <div class="form-group">
        <label class="col-sm-2 control-label"></label>
        <div class="col-sm-4">
            <div class="btn-group" data-toggle="buttons">
                <label class="btn btn-default">
                    <input type="radio" autocomplete="off" name="family" value="Bride" required />Bride
                </label>
                <label class="btn btn-default">
                    <input type="radio" autocomplete="off" name="family" value="Groom" required />Groom
                </label>
                <label class="btn btn-default">
                    <input type="radio" autocomplete="off" name="family" value="Friend" required />Friend
                </label>
                <span class="error"><?php echo $famErr;?></span>
            </div>
        </div>
    </div>
    <p class = "form-p">Guests in your party, including yourself: </p>
    <div class = "form-group">
        <label class="control-label col-sm-4"></label>
            <div class="col-xs-2">
                <input type = "text" class = "form-control" name="num" placeholder = "0" required />
                <span class="error"><?php echo $numErr;?></span>
            </div>
    </div>
</div> <!-- end of id relation-->

函数如下:

 // function to add RSVP user entry to the database
    public function user_attending_storage_RSVP($name, $email, $attend, $fam, $num){

        $replies = "INSERT INTO rsvp (name, attending, family, total) VALUES (:name,:attending,:family,:total)";

        try{
            $query = $this->conn->prepare($replies);
            $results = $query->execute(array(":name"=>$name, ":attending"=>$attend, ":family"=>$fam, ":total"=>$num));

        }
        catch(PDOException $e){
            die($e->getMessage());
        }   
    }

    // function to add RSVP user entry to the database
    public function user_not_attending_storage_RSVP($name, $email, $attend){



        $replies = "INSERT INTO rsvp (name, attending, family, total) VALUES (:name,:attending,:family,:total)";

        try{
            $query = $this->conn->prepare($replies);
            $results = $query->execute(array(":name"=>$name, ":attending"=>$attend, ":family"=>$fam, ":total"=>$num));  
        }
        catch(PDOException $e){
            die($e->getMessage());
        }   
    }

这是我在网页上调用函数的方式

 // check for data in fields
    if(isset($_POST['name']) ==true && isset($_POST['email']) ==true && isset($_POST['attending']) && isset($_POST['family']) && isset($_POST['num'])){

        $name=test_input($_POST['name']);
        $email=test_input($_POST['email']);
        $attend=test_input($_POST['attending']);
        $fam=test_input($_POST['family']);
        $num=test_input($_POST['num']);

        if($attend == "No" || $attend == "Not Sure"){

            $fam = "nothing";
            $num = 0;

            //inserting user data from form into database
            $genQuery->user_Not_attending_storage_RSVP($name, $email, $attend);

        }
        else{
            //inserting user data from form into database
            $genQuery->user_attending_storage_RSVP($name, $email, $attend, $fam, $num);
        }

        // send mail to user
        $genQuery->user_invite_confirmation_RSVP($name, $email, $attend,$fam, $num);


    }

【问题讨论】:

  • 你可以试试 if($('#input_id).val() == 'no' || $('#input_id).val() == 'not sure'){$( '#input_id_you_want_to_change).val('默认值')}
  • @Niall 这可能是一个不错的解决方案,但这是 javaScript。我将如何使用 javaScript 将这些数据存储在数据库中?
  • 那么你想做的是使用javascript将它存储在数据库中吗?那么你不能。您需要结合使用 javascript 和 php。使用: if(getElementById('input_id').value() == 'no' || getElementById('user_id') == 'not sure'){getElementById('input_you_want_to_change).value('Defualt value')}。更改输入中的值,然后使用 php 将其存储在数据库中,因为值现在已更改。
  • 不,我并不是在寻找一种专门使用 javascript 存储数据的方法,只是希望存储数据。非常感谢,不过我会试试你的方法。
  • 如果表单没有发布到服务器,您将如何处理?当你提交它时,你可以用这些数据做你想要的。此外,每次对“您确定”进行更改时,您都可以使用 ajax 来保留用户选择。但 IMO 是浪费资源。

标签: php jquery css twitter-bootstrap-3


【解决方案1】:

您可以使用 getElementById() 方法...

if(getElementById('input_id').value() == 'no' || getElementById('user_id').value() == 'not sure'){getElementById('input_you_want_to_change).value('Defualt value')}

如果用户选择否或不确定,这将更改值。然后使用 PHP 将这个新值存储到数据库中。

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 2016-02-07
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多