HTML get method wont send data to PHP - 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

Related

PHP Form to redirect with form results

I have a form that when they submit it, it stores the info in the database. I need to be able to get the form data to come up on redirect page.
It does not need to fetch the database as I would love to do this PHP style. So lets say they enter there name and city. When they click submit it redirects them to a thank-you page with the results from the form on that page.
In a form, you have each element have a name, lets say name="username", in the php, yould get the value of this as either a get, or a post response, depending on the method of the form.
HTML Form
<form action="process.php" method="get">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
or
<form action="process.php" method="post">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
process.php
$someusername = $_GET['username'];
$someusername = $_POST['username'];
Form page:
<form action="thanks.php" method="post"><input type="text" name="myname" placeholder="Name" /><input type="text" name="mycity" placeholder="City" /><input type="submit" value="submit"></form>
PHP Page
print "Thanks, ".$_POST['myname']." who lives in ".$_POST['mycity']."!";

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

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

How to send array by post method

Following is my sample form.
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="submit" NAME="submit" VALUE="Submit" />
</form>
Basically I have 10 inputs of array. Assume my domain is http://domain.com and the file above is index.php. I am trying to fill the form automatically by using the following method.
http://domain.com/index.php?array[]=John&array[]=Kelly ... & array[]=Steven
Unfortunately, it is not working. :(
Have you tried something like this:
<?php
foreach( $_GET['array'] as $arr ) // Loop through the `array` variables of GET
echo '<input type="text" name="array[]" value="' . $arr . '" />'; // Display the the inputs
?>
However, please make sure that you use a cleaning function on $arr to prevent XSS. You will also need to check if $_GET['array'] is set or not, or PHP will whine about it.
Two things to try.
You are using the GET method with your example URL, but your form is set to POST. In your PHP, you may be looking for your data in $_POST when it's actually in $_GET. You can get data from both POST and GET using the $_REQUEST variable.
You may need to urlencode the []. ([] becomes %5B%5D when urlencoded.)
Lastly, a tip: when crafting links to send data through a GET query string, it is best practice to use & in place of the &'s in the URL.
If you are trying to fill the form from a GET request (your URL string), you have to get the values from the HTTP request in the $_REQUEST array (or $_GET or $_POST depending on the form method). Suggestion: you should change the names of the form fields to reflect the value they are storing
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="first_name" value="<?php echo $_REQUEST['first_name']?>"/>
Note: accessing the $_REQUEST array as shown above is not good practice as you need to check if the request variables are set before you can echo their values.
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="first_name" value="<?php echo isset($_REQUEST['first_name'])?$_REQUEST['first_name']:""?>"/>

Automatically filling a form and submitting it doesn't get the parameters to the server

I have a form that is filled automatically by a JS
My Form is:
<form id="doSubmit" method="post">
<input type="text" id="usr" />
<input type="password" id="pwd" />
</form>
My JS is:
document.forms["doSubmit"].elements["usr"].value = usr;
document.forms["doSubmit"].elements["pwd"].value = psw;
document.forms["doSubmit"].action = location.pathname + "user";
document.forms["doSubmit"].submit();
Now on the next page which is PHP, when I try to use the I get this error Undefined index and when I type print_r($_POST) I get Array()
Any idea how I can post the form data on the next page?
Your input elements need to have a name attribute:
<form id="doSubmit" method="post" action="">
<input type="text" id="usr" name="usr" />
<input type="password" id="pwd" name="pwd" />
</form>
After you've done this, you should be able to see the usr and pwd keys in the $_POST array in your receiving PHP script. (The keys will be the HTML name attribute and the value will be the corresponding value HTML attribute.)
So, say your form ended up looking like this:
<form id="doSubmit" method="post" action="">
<input type="text" id="usr" name="usr" value="someuser"/>
<input type="password" id="pwd" name="pwd" value="somepassword"/>
</form>
Your $_POST array would look like this:
Array (
'usr' => 'someuser',
'pwd' => 'somepassword'
)
jsFiddle example

Categories