How do i get values of multiple inputs in php - php

I am writing an sql embedded in php database.
I am facing a problem when dealing with user inputs. I have a php file where the user can select the type of search. For example, select all data entries or limit the search to a given id num, name, last name...
This the code where the user selects type of search
<select name="type" >
<option value="fm"> Faculty Member Search </option>
<tr><td>List all Faculty Members<input type="checkbox" name="list[]" value="listAll" >
<div>
<label class="textClick"> List By Id<input type="checkbox" name="list[]" value="listId" > </label>
<input class="text" type="text" size = "8" placeholder="Insert Id" name="listById" > </div>
<div>
<label class="textClick">List By First Name <input type="checkbox" name="list[]" value="listName" ></label>
<input class="text" type="text" placeholder="Insert Name" name="list" >
</div>
<div>
<label class="textClick">List By Last Name<input type="checkbox" name="list[]" value="listFname" ></label>
<input class="text" type="text" placeholder="Insert Last Name" name="listLast" >
</div>
</select>
<input type="submit" value="Get Info">
Now in the other php file (where search is actually done) i can't seem to get the values of the user's inputs.. This the part where my code goes wrong
if($_POST["type"]=="fm"){
Print "LIST IS" . $_POST["list"];
if($_POST['list']=="listAll")
{
?>
<h1> Listing ALL Faculty Members </h1>";
<?php
this code gave me the following output:
LIST IS
I tried both $_POST['list'] and $_POST["list"] but my code just doesn't seem to pass the if condition

$_POST['list'] is going to be an array of the submitted values. You will need to loop through the array to get the values or access them like any other array value (i.e. $_POST['list'][0]) to get the first element.
Do var_dump($_POST['list']) and you'll see the array and its values.
What you probably want is only one value and should change the checkboxes to radio buttons and remove the array syntax from the input field names:
<label class="textClick">List By First Name <input type="rado" name="list" value="listName" ></label>

Related

HTML only posts last text box for text form

Here is my HTML code:
<form name="candidates" action="candidate_thankyou.php" method="post">
<div class="wrap">
<div class="position"> President: <br> </div>
<input type="text" name="president"> <br>
<input type="text" name="president"> <br>
<input type="text" name="president"> <br>
</div>
<div class="wrap">
<div class="position"> Vice President: <br> </div>
<input type="text" name="vp"> <br>
<input type="text" name="vp"> <br>
<input type="text" name="vp"> <br>
</div> ...etc
<input id="submit" type="submit" value="Submit">
</form>
However, when I call var_dump($_POST) in my php file, I only get the last text box value for each input name (eg "president" or "vp"). If I only use the first 2 text boxes, I get an empty string. How can I get all three values from the form?
Form fields are identified by their name. You're overriding the fields president/vp every time, that's why you only get the last field's values. You can send it as an array like this:
<input type="text" name="president[]">
<input type="text" name="president[]">
..
Field name must be either unique (president1, president2, etc.) or an array president[].
Further handling depends on which method you choose. Second method is well described in HTML form input tag name element array with JavaScript
You should have seperate names for each input or add [] to the end of the names to add them to an array. Ie
name="vp[]"

Inserting Multiple rows in Mysql table with PHP

I am in a fix here. I have code that does not insert multiple data into mysql table with one form. Here's my code-
<?php
if (isset($_POST['submitted'])) {
include('config.php');
foreach($_POST['name'] as $row=>$nam) {
$name=$nam;
$class=$_POST['class'][$row];
echo "<br/>" . $name . "<br/>" . $class;
$sql="INSERT INTO multiple (name, class) VALUES ('$name','$class')";
}
if (!mysql_query($sql)) die('Error: ' . mysql_error());
echo "1 record added";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Name1:
<input id="name" name="name[]" type="text">
<input type="hidden" name="submitted" value="true" />
</label>
<label> Class1:
<input id="class" name="class[]" type="text">
</label>
<label>Name2:
<input id="name" name="name[]" type="text">
</label>
<label>Class2:
<input id="class" name="class[]" type="text">
</label>
<label>Name3:
<input id="name" name="name[]" type="text">
</label>
<label>Class3:
<input id="class" name="class[]" type="text">
</label>
<label>Name4:
<input id="name" name="name[]" type="text">
</label>
<label>Class4:
<input id="class" name="class[]" type="text">
</label>
<label>Name5:
<input id="name" name="name[]" type="text">
</label>
<label>Class5:
<input id="class" name="class[]" type="text">
</label>
<input value="Add" type="submit">
</form>
When I press the submit button nothing inserts in the mysql table. Only empty fields are created. If I insert 5 text field I get 5 empty fields in sql table.
Imroz, your use of [] as part of the names of your input elements (not id's) example name="class[]" when the form is posted it builds an array. The post object PHP would recognize would be $_POST['class']
But that being an array means you have to handle it slightly different before inserting it into your database as you can't just (well maybe you can) drop an array into the DB
if you did
for($x=0;$x < count($_POST['class']); $x++)
{
echo $_POST['class'][$x].'<br>';
}
you would be able to see all your posted inputs from the inputs with the name class[]
of course this is a core example of what you need to do overall, but I am just trying to express whats going on with your posted data.
i think your inserting query is problem.....try like this
$query = mysql_query("INSERT INTO category VALUES('','$name','$class','')") or die(mysql_error());
You have the } in the wrong place.
The } right after the $sql= should be moved after the echo "1 record added";
I reformatting your code to use proper indenting and the error was obvious.

How to pass the data in the input type using php

Hi, I want to know how to pass the data into the input type field; I'm using only $_POST method and also I did not use the database first. Please help.
form.php
<form action="booking.php" method="post">
<p>
I want to be pick up at:
</p>
<p>
<input placeholder="Enter Address, Airport, Landmark" type="text" name="pickupplace" id="pickupplace" autocomplete="off" tabindex="1" style="width: 260px;"/>
</p>
<p>
drop off at
</p>
<p>
<input placeholder="Enter Address, Airport, Landmark" type="text" name="dropoff" id="dropoff" autocomplete="off" tabindex="2" style="width: 260px;" />
</p>
</form>
in the booking.php
<div>
<p>From</p>
<p>
<p>
<?php
if(isset($_POST['pickupplace']) == NULL){
echo "-";
}
else{
echo $_POST['pickupplace'];
}
?></p>
</div>
in the edit_ride.php
<div>
<p>From</p>
<p>
<input type="text" name="pickupplace" placeholder="Enter Address, Airport, Landmark"/>
</p>
</div>
i want to pass the data into the edit_ride.php in the input type please help here it is,
What you can do is, you can merge edit_ride.php and booking.php, so that directly you can display the values using
<input type="text" name="pickupplace" value="<?php echo $_POST['pickupplace']" placeholder="Enter Address, Airport, Landmark"/>
This may not be the only solution, as some time we need multiple pages, in that case you may consider sessions or cookies also.
Alternatively, you can pass the values from one page to another using querystring ($_GET) method also without submit the forms in internal pages.
why are you specifying the form action booking.php if you need to submit your data on edit ride.php
so first of all change the form action to edit_ride.php and then recieve the data in your textbox as following on the page edit_ride.php
<input type="text" value="<?php if(isset($_REQUEST['pickupplace'])) echo $_REQUEST['pickupplace']; ?>" name="pickupplace" placeholder="Enter Address, Airport, Landmark"/>

Many fields in HTML form

I have a form with about 150 fields, however the user can add more fields, reaching about 300 fields.
I'd like to know if exist a good way to put names and IDs to my fields, or i have to give each one a diferent name and ID. It's a hard job.
html with only one name for input
<form action="checkbox.php">
<input type="checkbox" name="data[]" value="1">1<br>
<input type="checkbox" name="data[]" value="2">2<br>
<input type="checkbox" name="data[]" value="3">3<br>
<input type="checkbox" name="data[]" value="4">4<br>
<input type="checkbox" name="data[]" value="5">5<br>
<input type="submit">
</form>
checkbox.php
$data = $_GET["data"]; //$data is an array with checked option
it works also for type text
<input type="text" name="text_data[1]"><br>
<input type="text" name="text_data[2]"><br>
<input type="text" name="text_data[3]"><br>
if i understand in right, then you can use same name,id for group of same types of control (group of textbox,checkbox,etc...) and then access as array from javascript or code behind ..

HTML Element Array, name="something[]" or name="something"?

I saw something on this site:
Handling array of HTML Form Elements in JavaScript and PHP
http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343
It said to put the array in the name property and how to get the input collection's value. For example, name="education[]"
But as I know, an HTML input element is array-ready by name.
On the client-side (GetElementsByName) or server-side ($_POST in PHP or Request.Form in ASP.NET).
For example: name="education", so what is the different with or
without the []?
PHP uses the square bracket syntax to convert form inputs into an array, so when you use name="education[]" you will get an array when you do this:
$educationValues = $_POST['education']; // Returns an array
print_r($educationValues); // Shows you all the values in the array
So for example:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
Will give you all entered values inside of the $_POST['education'] array.
In JavaScript, it is more efficient to get the element by id...
document.getElementById("education1");
The id doesn't have to match the name:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]" id="education1">
</p>
If you have checkboxes, you can pass an array of checked values.
<input type="checkbox" name="fruits[]" value="orange"/>
<input type="checkbox" name="fruits[]" value="apple"/>
<input type="checkbox" name="fruits[]" value="banana"/>
Also multiple select dropdowns
<select name="fruits[]" multiple>
<option>apple</option>
<option>orange</option>
<option>pear</option>
</select>
It's different.
If you post this form:
<input type="text" name="education[]" value="1">
<input type="text" name="education[]" value="2">
<input type="text" name="education[]" value="3">
you will get an array in PHP. In this example you will get $_POST['education'] = [1, 2, 3].
If you post this form without [],
<input type="text" name="education" value="1">
<input type="text" name="education" value="2">
<input type="text" name="education" value="3">
you will get the last value. Here you will get $_POST['education'] = 3.

Categories