How can I get the values from the PHP generated forms? - php

I dont know where or how to use $_POST to get the values of the forms.
This is the whole code, I uploaded it on GitHub if you want to see the whole thing
This is the file I am trying to get the values out of.
new.php
<?php
if (!empty($_POST) and isset($_POST['actiune']))
{
$actiune = isset($_POST['actiune']) ? $_POST['actiune'] : NULL;
switch($actiune){
//CASE 1
case 'addElev': echo '<div class=dbFormText>Adauga elev</div><div
class="databaseform"><form action="" method="post">
<input type="text" name="numeElev" placeholder="Nume elev"/>
<input type="text" name="orasElev" placeholder="Localitate"/>
<select name="sexElev">
<option value="Baiat">Baiat</option>
<option value="Fata">Fata</option></select>
<input type="text" name="tlfElev" placeholder="Nr. de telefon"/>
<input type="text" name="birthdayElev" placeholder="Nastere(DD-MM-YYYY)"/>
<input type="submit" value="Submit">
</form></div>';
break;
}
}
?>
This is the page of the first dropdown, which gives me the value of the switch
adminpanel.html
<div class="dropdowns">
<form action="" method="post">
<select name="actiune" onchange="this.form.submit()">
<option>Selecteaza actiunea</option>
<option value="addElev">Adauga elev</option>
<option value="addNota">Adauga nota</option>
<option value="addAbsenta">Adauga absenta</option>
<option value="addTeza">Adauga teza</option>
<option value="deleteElev">Sterge elev</option>
<option value="deleteNota">Sterge nota</option>
<option value="deleteAbsenta">Sterge absenta</option>
<option value="deleteTeza">Sterge teza</option>
<option value="modifyElev">Modifica elev</option>
<option value="modifyTeza">Modifica teza</option>
<option value="modifyPurtare">Modifica nota purtare</option>
</select>
<?php include('new.php') ?>
</div>

When you submit a form that has a method of post, e.g.
<form method="post">
(note: action="" is redundant and can be left out)
the target page (in this case, itself) will be provided with the $_POST array.
You can dump the entire array to look at the values by using:
if (!empty($_POST) and isset($_POST['actiune']))
{
var_dump($_POST);
}
This array should contain all the values from within the <form> tag, provided a name has been set (which will be used as the 'key' in the array).

Not sure what is the switch for, added the fetch of postvars at the top of code. I am sure you can do some php to make sure vars are set. Also actiune is not a postvar that is its not the name of anyof the input fields so dont do $_POST['actiune']. it wont work. Else you have to add a hidden field, I did it here for you.
<?php
if (!empty($_POST) {
$numeElev = $_POST['numeElev'];
$orasElev = $_POST['orasElev'];
$sexElev = $_POST['sexElev'];
}
if (!empty($_POST) and isset($_POST['actiune']))
{
$actiune = isset($_POST['actiune']) ? $_POST['actiune'] : NULL;
switch($actiune){
//CASE 1
case 'addElev': echo '<div class=dbFormText>Adauga elev</div><div
class="databaseform"><form action="" method="post">
<input type="text" name="numeElev" placeholder="Nume elev"/>
<input type="text" name="orasElev" placeholder="Localitate"/>
<input type="hidden" name="actiune" placeholder="1"/>
<select name="sexElev">
<option value="Baiat">Baiat</option>
<option value="Fata">Fata</option></select>
<input type="text" name="tlfElev" placeholder="Nr. de telefon"/>
<input type="text" name="birthdayElev" placeholder="Nastere(DD-MM-YYYY)"/>
<input type="submit" value="Submit">
</form></div>';
break;
}
}
?>

Related

How to maintain the PHP GET value on the address bar if I use another GET?

I am passing a variable from page1 to page2 using GET and it's already working. However, I also use another get in the <select> <option> on page2 which means when I click an item in the option, the value I get from page1 disappears and becomes undefined since the address bar changes. I want the variable I get from page1 to be compared on SQL where clause. I can't think of a way on how to solve this, I'm not really knowledgeable in PHP, can you help me?
Here is my code(I remove some unnecessary lines): page1
<form method="get" action="home.php">
<div class="form-group">
<input id="codehe" class="form-control" type="codehe" name="codehe" placeholder="Class Code">
</div>
<div class="form-group">
<input class="form-control button" type="submit">
</div>
</form>
page2 (this is how i get the value from page1)
<?php
$codehe = $_POST['codehe'];
echo $codehe;
?>
The other GET (form) on page2
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
</form>
<?php
if(isset($_GET['state'])) {
$str = $_GET["state"];
$sql = "SELECT * FROM classvideo WHERE subjects = '$str' AND linkcode = ''";
?>
The traditional way to do things like this (without client-side code) is to put a hidden input in your form on page 2:
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
<!-- Add this line -->
<input type="hidden" name="codehe" value="<?php echo $_GET['codehe'] %>">
</form>

PHP Form Multiple Inputs

I am having issues with php and form handling. I want to be able to click a dropdown box(like you can do with a form in html, with the different options tags) in php, and allow the selection to take the designated value.
Here is what I have :
<input value="<?php echo isset($results['example']) ? $results['example']: ''; ?>" class="form-control" name="data[example]" placeholder="Example">
That populates with a place you can type in input text, which is accepted perfectly in post, but I do not want the user typing in data, only selecting from options, such as this html form does :
<form action="example.php">
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</form>
Here is an edit using the data given :
<form>
<select name="example">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">oranges</option>
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">bananas</option>
</select>
</form>
I did not add the action because it will be posted with other data, but did try both ways(with and without action="post.php") and it does not work...
Defining your options inside a select tag
<select>
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</select>
Should give you in your post the position (value) of the selected item. Then in your php pass value to the selected items *11,12,13 = apples,bananas,oranges**
Hope it help
You need a <select name="name"> </select> around your options
I think you need something like that
<form action="example.php">
<select name="fruit">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
</select>
</form>
I'd say the issue is not PHP, I think you just need to use the SELECT and OPTION form tags:
You are specifying an INPUT tag, which by definition allows the user to input data.
<form action="example.php">
<select class="form-control" name="data[example]">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>"">
</select>
</form>
So you are looking for a dropdown I guess:
<form action="example.php">
<select name="fruit">
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value="3">Orange</option>
</select>
</form>
On submiting the form you can get the value like: $_POST['fruit'] (in this case).
use type="checkbox", so you can select one more
<input type="checkbox" name="vehicle[]" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle[]" value="Car"> I have a car<br>
<input type="checkbox" name="vehicle[]" value="Boat" checked> I have a boat<br>
in php
<?php
$vehicleArray='';
foreach($_POST['vehicle'] as $value) {
$vehicleArray.=$value.',';
}
//output 'Bike,Car,Boat'
?>

Form Post return no result, but GET works

I tried to change method to "GET" and its work, it print out in the URL, with the dateFrom and dateTo, but when I change to post and i do a print_r($_POST); the array is empty
What is wrong with my form
Thanks!!
<form action="" method="POST">
From <select name="dateFrom">
<option value="03-Sep-2014">03-Sep-2014</option>
<option value="31-Aug-2014">31-Aug-2014</option>
<option value="30-Aug-2014">30-Aug-2014</option>
</select>
To <select name="dateTo">
<option value="03-Sep-2014">03-Sep-2014</option>
<option value="31-Aug-2014">31-Aug-2014</option>
<option value="30-Aug-2014">30-Aug-2014</option>
</select>
<input type="submit" value="View">
</form>
And I did this to read my form
<?php
include '../inc/session_info.php';
$action = $_GET['action'];
$action_id = $_GET['action_id'];
//if filter by date
print_r($_POST);
$post_dateFrom = $_POST['dateFrom'];
$post_dateTo = $_POST['dateTo'];
echo $postDateFrom;
But nothing is echo out or print_r, array was empty
I change my
<form action="" method="POST">
to
<form action="" method="post">
din't know cap sensitive for the method. it ssolved my issues

Using select dropdowns in a form

I am trying to use HTML select dropdowns to help sort the search results on my website and I am having some trouble. In the code below, I specifically select Option1 and when I try to echo the result after I submit the form, it always echo's 'FAIL'. I was wondering if someone can help me understand what I am doing wrong and what is the proper way to retrieve the data from the option the user selected, after the form was submitted?
<?php
echo '<form method="post" action="search.php">
<select>
<option name="one">Option1 </option>
<option name="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
if(isset($_POST['one'])){
echo 'Option1 is set';
}else{
echo 'FAIL';
}
?>
thank you
This is because the name attribute goes with the select tag.
<select name="dropdown">
<option>Option 1</option>
<option>Option 1</option>
</select>
if(isset($_POST['dropdown'])) {
echo $_POST['dropdown']; //echoes 'Option 1'
}
If you like, you can add a value attribute to the option element, but you don't have to.
If you do
<option value="foobar">Option1</option>
The form will post foobar and not Option1.
<?php
echo '<form method="post" action="search.php">
<select name="my_option">
<option value="one">Option1 </option>
<option value="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
echo "My option value is : " . $_POST['my_option'];
?>
You need to name your select tag and access that instead.
<select name="options"></select>
echo $_POST['options']; will give you your selected option
Instead of name="one" an option needs a
value="myvalue"
and your select tag needs an
name="nameofthisthinghere"

Confused why form is not submitting

I don't think there's anything I left out, but the form doesn't seem to be transmitting any data upon hitting submit. My code is as follows, I realize it's kind of long, but I wanted to include the entire form. All of the attached functions just check if the input was valid:
<form name="formname" id="formname" action="database.php" method = post onsubmit="return checker()">
Username: <input type='text' id='un' onchange="checkname(id)" onkeyup="checkempty(id)" ><div id="un1"></div><br>
Reporter Title:<br>
<select id="s2" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="Assistant Director">Assistant Director</option>
<option value="Director">Director</option>
<option value="DB Admin">DB Admin</option>
<option value="Systems Admin">Systems Admin</option>
</select><div id="s21"></div>
<br>
Password: <input type='password' id='pw' onchange="checkpass(id)" onkeyup="checkempty(id)"><div id="pw1"></div><br>
Email: <input type='text' id='eml'onchange="checkemail(id)" onkeyup="checkempty(id)"><div id="eml1"></div><br>
Description:<br> <textarea rows="6" cols="20" id="desc" onchange="checkdesc(id)" onkeyup="checkempty(id)" ></textarea><div id="desc1"></div><br>
Type of Incident:<br>
<select id="s1" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="afs">afs</option>
<option value="password">password</option>
<option value="hardware">hardware</option>
<option value="other">other</option>
</select><div id="s11"></div>
<br>
<?php
include("connect.php");
$ret = mysql_query("SELECT * FROM countries");
echo "Choose your location:<br>";
echo "<select id='countries' onblur='checktype(id)'>";
echo "<option value='choose'>Choose one</option>";
while($array = mysql_fetch_array($ret)){
echo "<option value='{$array['abbrevs']}'>{$array['full']}</option>";
}
echo "</select><br>";
?>
<div id="countries1"></div><br>
<p>
Would you like an email copy?
<select id="s3">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<input type="submit" id = "sub" value= "Submit">
</p>
</form>
and the php I tried to receive it with
<?php
include("connect.php");
$username = $_GET['un'];
$password = $_GET['s2'];
$reporter = $_GET['pw'];
$email = $_GET['eml'];
$description = $_GET['desc'];
$type = $_GET['s1'];
$country = $_GET['countries'];
$emailopt = $_GET['s3'];
?>
the checker function:
function checker(){
if(isgood && isgood1 && isgood2 && isgood3 && isgood4 && isgood5 && isgood6)
{
return true;
}
else{
document.getElementById('subb').style.visibility="visible";
document.getElementById('subb').innerHTML = "You must fully complete the form.";
return false;
}
where the "isgoods" where just quick flags I made for validations, which was all working properly.
Your form's method is POST so you should use $_POST instead of $_GET in your PHP script.
Also you should fix up your HTML so it's valid, you need to quote the method attribute properly:
<form name="formname" id="formname" action="database.php" method="post" onsubmit="return checker()">
Well, I feel stupid. The problem was that I didn't give my HTML elements names, but rather just IDs. I was so used to using ajax and submitting by just getting the value with javascript via the ID that I forgot about names O.o
You are submitting your form with method="POST", so in your PHP you need to read the POST values:
$username = $_POST['un'];
...
Had you submitted your form with method="GET", your PHP code would work. Read about the difference here.

Categories