form data not getting passed - php

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

Related

How to save multiple form?

I have a form and a button ... when the button is pressed in the form of adding the following fields:
<input type="text" name="address" />
<input type="text" name="city" />
and if once again pressed the button in the form is again added field
I do not need to add in the estate []
<input type="text" name="address[]" />
<input type="text" name="city[]" />
to get an array, because each field additions are stored in another table
how to sava all data in database
and it's all on laravel framework
if you want to use the same Name element, then you'll need the brackets []
You can add an index $i like :
<input type="text" name="address[$i]" />
<input type="text" name="city[$i]" />
And then when you receive your data, you can use the same loop to get store your data in an array. Let me know if you need more details.

HOW to add increment value to $_POST['variable'] in php?

I am using dynamic form where user add more input text boxes for a certain field he want and the name of each box change with an increment like:
<form method="post" action="somescript.php">
<input type="text" name="textbox" />
<input type="text" name="textbox1" />
<input type="text" name="textbox2" />
<input type="text" name="textbox3" />
.... and so on
</form>
I want to echo these data following a loop:
<?PHP
$k=$_POST['counter']; //counter value coming as post variable
for($i=1$i<=$k;$k++){
echo $_POST['textbox'.$i]; //something like this......?
}
?>
Please reply.
Use array notation instead.
<form method="post" action="somescript.php">
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox][" />
.... and so on
</form>
When the form is submitted, $_POST['textbox'] will then be an array, and you can loop over it:
foreach ($_POST['textbox'] as $textbox) {
echo $textbox;
}
I just came across this issue because I had blocks of data that needed to be created dynamically and
echo $_POST["textbox$i"];
worked without the concatenation in it. Let me know if this is bad practice, it works in my situation though. The array way didn't work for me. Sorry for posting this on a 3 year old question. I'm not sure if that's bad practice. Thanks.

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

Automatic check if input filled

There are 10 boxes in my website that I fill up based on my needs. From php code I prefer not to check them one by one like this. Instead of that I thought it would be a good idea to put check marks in each boxes and if I fill something in input field it should be checked so I can check however many checkboxes are filled and know how many boxes are filled.
if ($input1) {$total = "1";
if ($input2) {$total = "2";
}
}
Anybody knows how can I put automatically check into a checkbox when I start typing anything in it ? But it should be unchecked back if I delete what I wrote before sending it. Or if you guys have a better idea that would be nice also.
Thank you !
you could do this in JavaScript. Basically if the number of input text is the same of the checkboxes then you could use this function for example:
function change() {
var input_lengths = document.getElementsByName("textArray[]");
for(var i= 0; i < input_lengths.length;i++) {
if(document.getElementsByName("textArray[]").item(i).value != "") {
document.getElementsByName("checkArray[]").item(i).checked = true;
}
}
}
and your html could be:
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="AAA" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="button" value="test" onclick="javascript:change()" />
why don't you name your checkboxes as an array e.g.:
<input type="checkbox" name="boxes[1]" />
<input type="checkbox" name="boxes[2]" />
then you can loop through the array in php and check each one individually
This is how i understood your problem:
you have 10 text inputs
you want to know how many of them contain actual input when they are submitted
your idea was to assign a checkbox input to each text input so that the corresponding checkboxes are checked when an input has text and is unchecked when an input has no text
Something very quick and dirty. You might want to name you inputs with a square bracket, so that php interprets that parameter as an array. Then you can iterate over an array and if the values are set you can count.
<html>
<body>
<?php
if(isset($_POST['input'])) {
$count = 0;
foreach($_POST['input'] as $value) {
if($value) {
$count++;
}
}
echo $count;
}
?>
<form action="testinputs.php" method="post">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input type="submit"/>
</form>
</body>
</html>
If you want to do that on the client side, than I'd take something like jQuery and look how many inputs on my page contain any text inside them.

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.

Categories