HTML forms with PHP script - php

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.

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>

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.

Related

Submit form but user's input remain

How to keep user's input after they submit a form?
For example:
<form action="a.php" method="POST">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
echo $firstname.$lastname;
?>
if user key in fname and lname, yes, it will echo out.. but i wan to keep their first name and last name at the input type, so that user dont need to refill again if the form is long.
You want to make your form sticky so that the user does not need to fill the entire form again which is a good practice.
You can add value to your form inputs for your form example like shown below:
<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])) echo $_POST['firstname'];?>" />
<input type="text" name="lastname" value="<?php if(isset($_POST['lastname'])) echo $_POST['lastname'];?>" />
In the similar way, you can make your entire form sticky (different for textarea and others but the similar idea).

How can I create registration form and how can I get what the users filled in that form in my FB app

I have created one app in facebook (using facebook canvas). It is working properly, but I want to know, how can I put a registration form with some fields???
I have written the code for form,
<form action="../form/fbform.php" method="post">
My Name
<input maxlength="50" name="name" type="text" value="" />
My Email
<input maxlength="50" name="email" type="text" value="" />
I Work for
<input maxlength="50" name="cmpny" type="text" value="" />
My Friend
<input maxlength="50" name="frdname" type="text" value="" />
My Friend's Email
<input maxlength="50" name="frdemail" type="text" value="" />
<input name="try" type="submit" value="Submit" />
</form>
That is the code for my form, but after submission it showing blank page.
In action I have written fbform.php, In that file, I have the written the sql code to insert these form details into my website database. and also I have written redirect url to my app. but it's not working.
How can put the registration form in my app and how can I get those details (what users filled in those fields) in my website database or some excel sheet??
I think you need to see the fbml tags to post your data to your desired web page and try the GET method and use the session object of the facebook to retrieve your details .
<form action="../form/fbform.php" method="POST">
<input type="text" name="test1"></input>
<input type="hidden" name="fb_sig_session_key" value=”<?php $_GET[‘fb_sig_session_key’]”></input>
<input type="hidden" name="user_id" value=”<?php $_GET[‘user_id’]”></input>
<input type="submit" name="Submit"/>
</form>

HTML get method wont send data to PHP

Data inside my form will not send to search.php.
<form class="form-search" action="search.php" method="get">
<input type="text">
<button type="submit">Search</button>
</form>
// search.php
<h2>Searched <?php print $_GET['username']; ?>; Returned 5 Results from Query.</h2>
Notice: Undefined index: username in C:\xampp\htdocs\Web\Statistics\search.php on line 40
Therefore, the data was not set in $_GET.
You need to add a name to the input
<input type="text" name='username' placeholder="Username" id="username" class="input-medium search-query">
$_GET works based on the Input name not the ID. Name is passed when you press submit, ID just identifies the input on the page.
You need name="username" in addition to all of the other attributes you've specified.
your form should look like
<form class="form-search" action="search.php" method="get">
<fieldset>
<input type="text" name="username" placeholder="Username" id="username" class="input-medium search-query">
<button type="submit" id="srchUser" class="btn">Search</button>
</fieldset>
</form>
I've added to your input name="username" so it can send get variables to php by using the name attribute in input items

form data not getting passed

I'm practicing form validation with JavaScript but when I try to retrieve the data from the page it submits to I can't get it.
form.html
<body>
Hello.<br />
<form onsubmit="return validate()" action="process.php" method="POST">
Enter name: <input type="text" id="name" /><br />
Enter phone number: <input type="text" id="number" /><br />
Enter password: <input type="password" id="paswd" /><br />
Is there anything else you would like to add: <input type="text" id="anything" /><br />
<input type="submit" value="Check Form" />
</form>
</body>
process.php
<?php
echo 'Here: '.$_POST['number']
?>
Whatever index I use I get " Undefined index: line 2". What am I doing wrong?
EDIT: So I can't use the id attribute I need the name? Is there anyway to prevent coding redundancy since the value of all names will be the same as the corresponding id?
You need name attribute in your fields
<input type="text" id="number" name="number" />
$_POST looks for the name attribute in the field to capture the field values and not id
Your inputs need the name of the element.
Such as:
<input type="text" id="number" name="number" />
The Php gets the form data looking these names, not the ids.
you forgot name of input:
<input type="text" id="number" name="number" />
You need to give your form elements names.
<input type="password" id="paswd" name="paswd" />
Interestingly names and ids share the same namespace. If you don't really need the ids, leave them be. Inside a validate function you can always access all elements with the elements object of the form.
// called onsubmit
var validate = function(e) {
if (this.elements["paswd"].value.length < 4) {
alert("password needs to have at least 4 characters");
return false;
}
return true
};
I usually append the input type to my ids to differentiate them from field names
<label for="paswd-txt">Password: </label>
<input type="text" name="paswd" id="paswd-txt" />
<label for="save-cb">Remember me: </label>
<input type="checkbox" name="save" id="save-cb" value="1"/>
So like Vitor Braga said your inputs need the name of the element, but you only need this if you are using PHP to hadle the values of form in the submit, if you are using javascript to validaate like you said your were praticing you can obtain the value like this:
document.getElementById("number").value

how to pass values from one page to another on jquery form submit

I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.

Categories