Nick
Nick

Reputation: 1793

HTML forms with PHP script

I am trying to enter a record in the database. This record consists of the following : 1) Unique facebook id 2) name 3) Favorite car of the person

While the first parameter I get using the graph API from facebook, the other two are entered by the user using HTML forms using some code like this :

<form action="insert.php" method="post">
            Name <input type="text" name="Name" /></br>
            Fav car: <input type="text" name="car" /></br>
            <input type="submit" />
        </form>

Now the "insert.php" script will insert the record with the database. How can I pass the first parameter, i.e. the ID, which is something that insert.php needs, but it is not a user-entered parameter.

Upvotes: 1

Views: 123

Answers (2)

Julien Bourdon
Julien Bourdon

Reputation: 1723

Use a hidden parameter as follows (I assume your PHP variable containing the ID is $facebookId):

<form action="insert.php" method="post">
   <input type="hidden" name="facebook_id" id="facebook_id" value="<?php echo $facebookId?>"/>
   Name <input type="text" name="Name" /></br>
   Fav car: <input type="text" name="car" /></br>
   <input type="submit" />
</form>

You could also use pure Javascript (I assume you stored the Facebook GraphID response in facebookResponse)

<script type="text/javascript">
   $(document).ready(function() {
       document.getElementById("facebook_id").value = facebookResponse["id"];
   }
</script>

Upvotes: 2

Ayush
Ayush

Reputation: 42450

Pass it as a hidden input from within your form.

<form action="insert.php" method="post">
    Name <input type="text" name="Name" /></br>
    Fav car: <input type="text" name="car" /></br>
    <input name="fbID" type="hidden" value="{value_to_pass_here}" />
    <input type="submit" />
</form>

This way the fbID input won't be exposed in the front end of your website but will get POSTed to your server side script along with other input parameters, and you can access it in index.php at $_POST['fbID'];

The value you want to pass, in your case the FB Key, should be in the value attribute of this input field.

Upvotes: 1

Related Questions