您可以通过php函数将值分配给表单中的隐藏字段,以存储在数据库中吗?

  1. I created a form you know; text fields, radio buttons and the submit button
  2. within the said form, I have a div enclosing a section of the radio buttons hidden and a text field upon page load using inline CSS display:none;
  3. If the end user chose yes, the hidden fields will be displayed using a jquery function. If the user chose No or Not Sure, the form will remain hidden or become hidden using the same jquery function.
  4. If the user chose No or Not Sure, i want to automatically assign values for the hidden fields and store them in database.

Here is my form:

<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-->

Here are the functions:

 // 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());
        }   
    }

Here's how I call the function on the webpage

 // 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);


    }

You can use the getElementById() method...

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

This will change the value if the user selects no or not sure. Then use PHP to store this new value in the database.