Form that uses array, assign object to array - php

I need to create a form that allows the user to select a number from 0 to 7 and "show all" and the script will then output the object found in that train car to the user, or if show all is picked then it runs a loop that shows all 8 objects along with the train number. But my code doesn't really run.
(Please ignore my code if it doesn't make any sense, but I don't seem to get arrays :( Please point out the mistakes and thank you so so so much)
<html>
<body>
<form name="train" method="GET" action="test.php">
<select>
<option value="0" name="object">0</option>
<option value="1" name="object">1</option>
<option value="2" name="object">2</option>
<option value="3" name="object">3</option>
<option value="4" name="object">4</option>
<option value="5" name="object">5</option>
<option value="6" name="object">6</option>
<option value="7" name="object">7</option>
<option value="8" name="object">8</option>
<option value="all" name="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] ="pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
$train = $_GET['object'];
echo "<p>I have $train!</p>";
}
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
?>
</body>
</html>

There are some mistakes in the code
Select should have name attribute
Need to get object and display results based on the selected option.
Use the below code
<form name="train" method="GET" >
<select name="object">
<option value="0">0</option>
......
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
PHP code
$train[0] ="pencil";
....
$train[8] = "cup";
if ($_GET['submit']) {
$object = $_GET['object'];
if($object == 'all'){
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
}else{
echo "<p>I have $train[$object]!</p>";
}
}

Related

PHP: How to carry over session variables between 3 pages?

So as of now, i can successfully get the results to move from page one to page two using post and get, but no matter what im doing it will not move the info to the 3rd page. Im trying to switch it over to sessions after reading its made exactly for this but for some reason im doing something wrong and after hours of searching i cant for the life of me figure out what it is. I've followed guides, followed videos, and other post related to the topic on this website. I have now come to the conclusion that it is just me and i need some assistance. Any help would be greatly appreciated.
Page 1 (Index Page | Input Your Variables):
<?php session_start();
$_GET['q'] = $q;
$_GET['s'] = $s;
?>
<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>
Page 2 (Search.php that will take you directly to page specified if its already been created):
<?php session_start();
$q = $_POST['q'];
$s = $_POST['s'];
?>
<?php
$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
echo "<a href='$dir/$s/$q.htm'>$file</a>";
}
}
closedir($res);
?>
<?php
echo $htmlHeader;
while($stuff){
echo $stuff;
}
echo "<script>window.location =
'http://www.somesite.com/$dir/$s/$q.htm'</script>";
?>
Page 3 (404 page for catch all that are not in the system):
<?php session_start();
?>
<form action="" method="" name="FormChoice">
<input name="q" maxlength="8" type="text" value="<?php echo $_POST['q']; ?>" id="q" required>
<select name="s" id="s" required aria-required="true">
<option value="" disabled>CHOOSE STATE</option>
<option value="AL" <?php if($_POST['s'] == al) {echo ' selected="selected"';} ?>>ALABAMA</option>
<option value="AK" <?php if($_POST['s'] == ak) {echo ' selected="selected"';} ?>>ALASKA</option>
<option value="AZ" <?php if($_POST['s'] == az) {echo ' selected="selected"';} ?>>ARIZONA</option>
<option value="AR" <?php if($_POST['s'] == ar) {echo ' selected="selected"';} ?>>ARKANSAS</option>
<option value="CA" <?php if($_POST['s'] == ca) {echo ' selected="selected"';} ?>>CALIFORNIA</option>
<option value="CO" <?php if($_POST['s'] == co) {echo ' selected="selected"';} ?>>COLORADO</option>
<option value="CT" <?php if($_POST['s'] == ct) {echo ' selected="selected"';} ?>>CONNECTICUT</option>
</select>
<input type="submit" id="submitbtn2" value="SEARCH" name="submit" OnClick="search()" />
<span id="or">OR</span>
<input type="submit" id="addbtn" value="ADD" name="submit" OnClick="add()" />
</form>
page1
<?php
session_start();
// next 2 lines do NOTHING remove them
// as you have not yet loaded any values into $q and $s
//$_GET['q'] = $q;
//$_GET['s'] = $s;
?>
<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>
Page 2 - Search - receives data from previous form
- Contains lots of unecessary <?php...?>
- Previous form uses method="get" so data will arrive in the $_GET array not the $_POST array
<?php
session_start();
//$q = $_POST['q'];
//$s = $_POST['s'];
// But this is silly as you have not yet tested these values exist
// but you do that in the next lines
//$q = $_GET['q'];
//$s = $_GET['s'];
$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : '';
$res = opendir($dir);
// Now if you want to pass the values of `q` and `s` on to the next form
// they now need to be added to the session
$_SESSION['q'] = $q;
$_SESSION['s'] = $s;
while(false!== ($file = readdir($res))) {
if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
echo "<a href='$dir/$s/$q.htm'>$file</a>";
}
}
closedir($res);
echo $htmlHeader;
while($stuff){
echo $stuff;
}
echo "<script>
window.location = 'http://www.somesite.com/$dir/$s/$q.htm';
</script>";
// added missing semi colon ^
?>
Page 3 (404 page for catch all that are not in the system):
Now the data will be available in the SESSION, when you get to this page.

Adding radio button to MySQL fetch result and use result value to fetch another record...?

I want a first radio button to be checked by default. When I click get schedule I want to fetch the schedule of that radio selected train number using a WHERE clause.
<?php
while ($res = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><input type = radio />" . $res['Train_no'] . "</td>";
?>
<form action="schedule.php" method="POST" class="form-inline">
<input type="submit" value="Get Schedule"/>
</form>
<?php
}
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('traindb', $con);
} else {
die('Could not connect: ' . mysql_error());
}
$selected_val = $_POST['Train_no']; // Storing Selected Value In Variable
echo "You have selected :" . $selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE Train_No='$selected_val'");
mysql_close($con);
?>
You can try this:
if(isset($_POST['Submit'])){
$selected_val = $_POST['Train_no']; // Storing Selected Value In
echo "You have selected :" .$selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE
Train_No='$selected_val'");
}
Here is one example of "select":
<form action="" method="post">
<select type="age" class="form-control" id="age" name="age">
<!-- <option value="disable" selected="">Please Select</option> -->
<option value="">Please select</option>
<option value="Under 35">Under 35</option>
<option value=">35 - 44">35 - 44</option>
<option value=">45 - 54">45 - 54</option>
<option value=">55 - 59">55 - 59</option>
<option value=">60 - 64">60 - 64</option>
<option value=">65 - 69">65 - 69</option>
<option value=">70 - 74">70 - 74</option>
<option value=">75 - 79">75 - 79</option>
<option value="80 +">80 +</option>
</select>
<input type="submit" value="Submit">
</form>
<?php
$age = $_POST['age'];
if (isset($_POST['age']) && $_POST['age'] == "")
echo "You did not choose any options. Pls try again.";
else {
echo $age;
}
?>
If you want embed PHP in select options, do it like this:
<option value="<?php echo $res['Train_no'] ?>"><?php echo $res['some_other'] ?></option>
For radio button it is like this:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<input type="radio" name="gender" value="<?php echo $res['Train_no'] ?>" checked><?php echo $res['some_other'] ?><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br><br>
<input type="submit">
</form>
</body>
</html>

how to change select tag values based on value coming from database

i want to change tag option based on values coming from database.if value is "1" i want to show different options and different for other values.
<<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
Because
No closing ; on here <option value='4'>Maths</option></select>"
and here too <option value='8'>Maths</option></select>"
First if() condition missing end }.
So final code is
<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number = $_POST['number'];
//echo $number;
if ( $number == "1" )
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
You are missing the ; off of the end of both echos:
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
Note: Sorry about poor layout, quick posting of answer!
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"; //missing ; here
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"; //missing ; here
}
} // missing } here

wordpress form with roster registration of users

I'm trying to build a manual form on a Wordpress page.
My goal is to create a dropdown menu of all the users with a fixed role (in my case "athlete") and register single users in some sport events.
My code:
<form name="team_captain" method="POST" >
Carnival Name: <input type="text" id="carnival_name" name="carnival_name" />
<input type="submit" name="submit_carnival" value="Create Carnival"/><br />
Event Name: <select name="event_name">
<option value="Ironman Race">Ironman Race</option>
<option value="Board Race">Board Race</option>
<option value="Ski Race">Ski Race</option>
<option value="Surf Race">Surf Race</option>
</select><br />
Athlete Name:
<select name="athlete_name">[insert_php] echo getUsersByRole('athlete','athlete-selector'); [/insert_php]</select><br />
Age Group: <select name="age_group">
<option value="Under 15">Under 15</option>
<option value="Under 17">Under 17</option>
<option value="Under 19">Under 19</option>
<option value="Open">Open</option>
</select><br />
Sex: <input type="radio" name="athlete_sex" value="Male">Male <input type="radio" name="athlete_sex" value="Female">Female<br />
<input type="submit" value="Insert Race"/>
</form>
[insert_php]
if(isset($_POST['submit_carnival'])) {
$carnival_name = $_POST['carnival_name'];
echo "Registrations for ".$carnival_name;
}
elseif(isset($_POST['submit'])) {
$event_name = $_POST["event_name"];
$athlete_sex = $_POST["athlete_sex"];
$age_group = $_POST["age_group"];
$athlete_name = $_POST["athlete_name"];
echo $event_name;
echo $age_group;
echo $athlete_sex;
echo $athtlete_name;
}
[/insert_php]
At this point I can display the name of the carnival, but I can't display the instances of the athletes. And the dropdown list of the athletes doesn't work.
This is the function:
function getUsersByRole($role,$name,$selected = '',$extra = '') {
global $wpdb;
$wp_user_search = new WP_User_Query(array("role"=> $role));
$role_data = $wp_user_search->get_results();
foreach($role_data as $item){
$role_data_ids[] = $item->ID;
}
$ids = implode(',', $role_data_ids);
$r = $wpdb->get_results("SELECT * from ".$wpdb->prefix . "users where id IN(".$ids .")", ARRAY_A);
$content .='<select name="'.$name.'" id="'.$name.'" '.$extra.'>';
if($selected == ''){
$content .='<option value="" selected="selected">Choose a user</option>';
}else{
$r_user = $wpdb->get_results("SELECT * from ".$wpdb->prefix . "users where ID = ".$selected."", ARRAY_A);
$content .='<option value="'.$selected.'" selected="selected">'.stripslashes($r_user[0]['display_name']).'</option>';
}
for($i=0; $i<count($r); $i++){
$content .='<option value="'.$r[$i]['ID'].'">'.stripslashes($r[$i]['display_name']).'</option>';
}
$content .='</select>';
return $content;
}
Because you're looking for a $_POST["submit"], you'll have to have an input with the name attribute equal to submit. In your case replace this:
<input type="submit" value="Insert Race"/>
with this:
<input type="submit" name="submit" value="Insert Race"/>
Your getUsersByRole() function is already including the "select" tag, so replace this line:
<select name="athlete_name">[insert_php] echo getUsersByRole('athlete','athlete-selector'); [/insert_php]</select><br />
with this, notice that I changed "athlete-selector" to "athlete_name":
[insert_php] echo getUsersByRole('athlete','athlete_name'); [/insert_php]<br />
With those two changes, things should be working, though I haven't studied your getUsersByRole() function intensively, so there could be a bug in there. Let me know in a comment below if this works for you or not.

Get Array Value

I have one form that have a select box inside while-loop. I try to get the data or value from that select box. Below is my form code.
<form role="form" method="post" action="test2.php" >
<?php
$endDate = '2014-01-28';
$startDate = '2014-01-27';
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startDate ) ) / 86400 );
$x=0;
while($x<=$daydiff)
{
?>
<select class="form-control" name="day_<?php echo $x; ?>">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
</select>
<select class="form-control" name="time_<?php echo $x; ?>">
<option value="10am">10am</option>
<option value="11am">11am</option>
<option value="12pm">12pm</option>
<option value="1pm">1pm</option>
</select>
<?php
$x++;
}
?>
<input type="hidden" name="count" value="<?php echo $x ?>"/>
<button type="submit" name="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
</form>
Below is my "test2.php".
<?php
for ($i = 0; $i < $_POST['count']; $i++){
$day_var = 'day_' . $i;
$days_list[] = $_POST[$day_var];
$time_var = 'time_' . $i;
$time_list[] = $_POST[$time_var];
echo ($_POST[$day_var]);
echo ($_POST[$time_var]);
}
?>
When I submit it will echo "monday10tuesday11am" how can I extract the value separately i.e. monday10 and tuesday11am?
You can replace your numbering scheme and variable variables with a simple array in HTML.
<select class="form-control" name="day[<?php echo $x; ?>]">
<select class="form-control" name="time[<?php echo $x; ?>]">
$days_list[] = $_POST[$day];
$time_list[] = $_POST[$time];
This would greatly simplify your form2.php. Otherwise, please try to explain what results you are expecting with the code you have.
just add some space to your echo.
echo ($_POST[$day_var]).' '.($_POST[$time_var]);

Categories