making site id increment by 1 - php

Hey guys im trying to make a quiz and currently I have ran into an issue, I have made it increment but not send the data, so I need to fix it but don't really know how to, I need the users to be able to select an option the data get sent to the next page but the page must also increase in increment.
this is what I have got
<form name="form2" method="post" >
<select name="answer">
<option value=$myrow['A']><?php echo $myrow['A']?></option>
<option value=$myrow['B']><?php echo $myrow['B']?></option>
<option value=$myrow['C']><?php echo $myrow['C']?></option>
<option value=$myrow['D']><?php echo $myrow['D']?></option>
<input type="submit" name="submit" value="Continue" onclick = "window.location.href = 'testttts.php?id=<?php echo $count?>'"
any help would be great thank you!!

Is this what you mean?
<?php
if(isset($_POST['i'])) {
$i = $_POST['i'];
} else {
$i = 0;
}
echo "increment: " . $i;
$i++;
?>
<form name="form2" method="post" >
<select name="answer">
<option value=$myrow['A']><?php echo $myrow['A']?></option>
<option value=$myrow['B']><?php echo $myrow['B']?></option>
<option value=$myrow['C']><?php echo $myrow['C']?></option>
<option value=$myrow['D']><?php echo $myrow['D']?></option>
</select>
<input type="hidden" name="i" value="<?php echo $i; ?>" />
<input type="submit" name="submit" value="Continue" />

Related

keep selected values in select box field after search

I want to keep the selected values in select box after search .This is a vehicle search filter page and Page is coded using php and connected with database.
Database have 2 columns for both options that is driver age for 21-24 and driver_age for 25+ as some vehicles also available for both age of drivers
code below
<script>
$('#driver').change(function() {
if($('#driver option:selected').val() == '21-24') {
$('#driver').attr('name','driverage');
}
if($('#driver option:selected').val() == '25+') {
$('#driver').attr('name','driver_age');
}
});
</script>
<form id="myform" method="GET" action="">
<div class="fields">
<p>Vehicle Type</p>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<?php if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){ ?>
<option value="<?php echo $vt ?>" <?=isset($_GET['car_type'])&&$_GET['car_type']==$vt?'selected':'';?> ><?php echo $vt ?></option>
<?php }
} ?>
</select>
</div>
<div class="fields">
<p>Age of Youngest Driver</p>
<select class="half" id="driver" name="">
<option value="21-24">21-24</option>
<option value="25+">25+</option>
</select>
</div>
<input type="hidden" name="search" />
<div class="fields" style="text-align:center">
<input type="submit" value="Search" class="mitsub" />
</div>
</form>
Thanks and please help
You can add a conditional statement inside your foreach() that adds selected attribute to option. This will ensure that the selected value is selected in the list of options.
<?php
if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){
echo "<option value='$vt'";
if(isset($_GET['car_type']) && $_GET['car_type'] == $vt){
echo " selected";
}
echo ">$vt</option>\r\n";
}
}
?>
You can do this all in one, you just have to make sure the logic is correct.
echo "<option value='$vt'" . ((isset($_GET['car_type'] && $_GET['car_type'] == $vt) ? " selected" : "") . ">$vt</option>\r\n";
I just find it easier to work with a full if statement.

how to create php multiple forms using option dropdown?

can i create multiple form look like the one in phpmyadmin where the user can
create multiple forms by selecting a number from option dropdown list to insert multiple value
<form>
<input type="text" />
<input type="text" />
<input type="text" />
</form>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
this is the code i am using
<form action="ChangeManager.php" method="post">
<select name="prog_lang">
<?php
if($questions = $num_of_questions){
$num_of_questions = 1;
for ($i=1; $i<=$num_of_questions; $i = $i + 1){
echo "<label>السؤال رقم #".$i."</label><br>";
echo "<textarea name='"."q".$i."' rows='10' cols='50'></textarea><br>";
echo "<label>الاجابات الممكنة:</label><br>";
for($j=1; $j<=4; $j = $j + 1){
echo '<input class="choice" type="text" name="'.'choice'.$i.$j.'" id="choice'.$i.$j.'"/>';
echo '<input class="correct" type="radio" name="'.'correct'.$i.'" value="correct'.$i.$j.'"/>';
echo '<label>صحيحة</label><br>';
}
echo "<br><br>";
}
}
?>
</select>
<input style="color:blue; font-size:14pt; width:100;"type="submit" value="حفظ الاختبار" />
</form>
I suggest to use JavaScript in order to manipulate the DOM. However you can do it with PHP as well:
First you need to know how many input:
<form action="" method="post">
<select name="howManyInput">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</form>
In the 'action' page you get the value of 'howManyInput':
<?php
$howMany = $_POST['howManyInput'];
?>
Then you print the input you need in a 'for' loop:
<form action="" method="post">
<?php
for( $i=0; $i<$howMany; $i++ ){
echo '<input name="inputName'.$i.'" >';
// if you need more the 1 input for '$howMany' echo it here
}
?>
</form>
The first form take you to the page where the user can insert data.
In that page you print as many input the user select in the previous page.
When the second form is submitted it goes to a page that receive all data.

Action Form not work

<form action="/en2/maps-<?php echo $id; ?>/?=. ($_GET['textSearchTerms']).&=. ($_GET['locationSearchTerms'])."><!--Relative url to the page that your map is on-->
Distination:
<select name="textSearchTerms" class="selectpicker" data-live-search="true">
<option value="alfamart">Alfamart</option>
<option value="BCA">BCA</option>
</select>
Location:
<input type="text" name="locationSearchTerms">
<input type="submit" value="Search">
</form>
<form action="/en2/maps-<?php echo $id; ?> not work...
any idea?
I want this link for my form action:
http://indonesia.com/en2/maps-alfamart/?textSearchTerms=alfamart&locationSearchTerms=denpasar
thanks
Use like this
<?php
$id="alfamart";
$textSearchTerms = isset($_GET['textSearchTerms']) ? $_GET['textSearchTerms'] : "";
$locationSearchTerms = isset($_GET['locationSearchTerms']) ? $_GET['locationSearchTerms'] : "";
$url = "/en2/maps-".$id."/?textSearchTerms=".$textSearchTerms."&locationSearchTerms=".$locationSearchTerms;
?>
<form action="<?php echo $url; ?>" method="GET">
Distination:
<select name="textSearchTerms" class="selectpicker" data-live-search="true">
<option value="alfamart">Alfamart</option>
<option value="BCA">BCA</option>
</select>
Location:
<input type="text" name="locationSearchTerms">
<input type="submit" value="Search">
</form>
Added form method GET and your php variable in php tag
Replace
<form action="/en2/maps-<?php echo $id; ?>/?=. ($_GET['textSearchTerms']).&=. ($_GET['locationSearchTerms']).">
with
<form action="/en2/maps-<?php echo $id; ?>/?textSearchTerms=<?php echo $_GET['textSearchTerms']; ?>&locationSearchTerms=<?php echo $_GET['locationSearchTerms']; ?>">
Note:- You missed textSearchTerms and locationSearchTerms in your form action parameter before =.

make an option from select tag stays the same after reload or change the page, PHP

So im having problems on figuring out how to make option tag so when is pressed it will make the action on all the pages
This is what i have done:
index1.php
include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>
index2.php
include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>
select.php
<div id="regContainer">
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option value="miami">miami</option>
<option value="new york">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
</div>
process.php
<?php if(!isset($_SESSION)){ session_start();}
if(isset($_POST['selectedRegion'])){
$region = $_POST['selectedRegion'];
if($region == "miami"){
echo "miami selected";
$title = "miami";
}
if($region == "new york"){
echo "new york selected";
$title = "new york";
}
}
?>
when I press the first option, it changes the title normally on the index1.php, but when i go to the index2.php... it does not change the title of that page, I have to press the first option button again to make the change.
Is there anyway to just press the first option from the selection and then change the title of all the pages without having to press the button again?
Any suggestion will help. Thanks
Using the code you currently have you could do the following
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option <?php if($_REQUEST['selectedRegion'] == 'miami') echo "selected"; ?> value="miami">miami</option>
<option <?php if($_REQUEST['selectedRegion'] == 'newyork') echo "selected"; ?> value="newyork">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
This will ensure the option is selected on page refresh. If you want something that also acts on page change then you will need to use a cookie
if(isset($_REQUEST['selectedRegion'])){
setcookie("region", $_REQUEST['selectedRegion'], time()+3600);
}
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option <?php if($_COOKIE["region"] == 'miami') echo "selected"; ?> value="miami">miami</option>
<option <?php if($_COOKIE["region"] == 'newyork') echo "selected"; ?> value="newyork">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
You need to change
if(!isset($_SESSION)){ session_start();}
To just
session_start();
What you're saying currently is if you have a session set, don't load the session library to then use that session data.

Inserting Multiple Rows by Form into a Single DB

First time doing this, watch it be a really obvious/easy fix...
Basically I'm trying to insert multiple rows with a single insert query, however, I clearly fail and as such it wont work.
The form is generated by searching a table for the relevant entries it relates to, so if your in department A it will grab your name and form an input line on the form. This all works fine I can generate the form however the issue them comes when I try to insert the form.
Currently it does this thing where rather than inserting the data entered via the form it is entering the letter A across most fields (COUNT, STATUS and TYPE are immune to this as they are restricted to only certain data types via the tables set up itself).
This is currently how I have the form set in the script:
<?php
if ($action == "editrecord") {
?>
<form name="form1" method="post" action="">
<table>
<tr><td colspan="2" align="left">
<?php //Getting Title for Record
$resultTITLE = queryDB("SELECT * FROM merit_subdepts WHERE subdeptID = '$subdeptID'");
while($rowTITLE = mysql_fetch_array($resultTITLE)) {
$recordTITLE = $rowTITLE["subdeptNAME"];
echo "$recordTITLE";
}
?>
</td></tr>
<tr><td>Member Name</td><td>Choose Award</td><td>Reason</td><td></td></tr>
<?php
$resultMERIT = queryDB("SELECT * FROM merit_members WHERE memberPRIMARY = '$subdeptID'");
while($rowMERIT = mysql_fetch_array($resultMERIT)) {
$memberID = $rowMERIT["memberID"];
$memberNAME = $rowMERIT["memberNAME"];
?>
<tr>
<td><?php echo $memberNAME; ?></td>
<td><select name="count[]" class="ui-state-default">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
<td><input name="reason[]" type="text" id="reason" class="blackInput" value="Monthly Award <?php echo date("F, Y") ?>"></td>
<td><input name="type[]" type="hidden" id="type" value="Civil">
<input name="awarder[]" type="hidden" id="awarder" value="<?php $usersname; ?>">
<input name="datea[]" type="hidden" id="datea" value="<?php $stamp83; ?>">
<input name="status[]" type="hidden" id="status" value="Check">
<input name="id[]" type="hidden" id="id" value"<?php $memberID; ?>">
<input name="dept[]" type="hidden" id="dept" value"<?php $recordTITLE; ?>">
</td>
</tr>
<?php } //end of sub-division membership list ?>
<tr><td colspan="4"><center>
<div class="butDiv"><input name="submitRECORDS" type="submit" id="submitRECORDS" value="Submit Records"></div>
</center>
</td>
</tr>
</table>
</form>
<?php
} //End of Edit Merit Records
?>
This is the insert query used:
if ($submitRECORDS) {
for($i = 0; $i<count($_POST['id']); $i++) //Getting total # var submitted and running loop
{ $fromID = $_POST['id'][$i]; //var
$fromTYPE = $_POST['type'][$i]; //var
$fromCOUNT = $_POST['count'][$i]; //var
$fromDATEA = $_POST['datea'][$i]; //var
$fromAWARDER = $_POST['awarder'][$i]; //var
$fromREASON = $_POST['reason'][$i]; //var
$fromDEPT = $_POST['dept'][$i]; //var
$fromSTATUS = $_POST['status'][$i]; //var
if ($fromID != "0") {
queryDB("INSERT INTO merit_precord (NAME,TYPE,COUNT,DATEA,AWARDER,REASON,DEPT,STATUS) VALUES ('$fromID','$fromTYPE','$fromCOUNT','$fromDATEA','$fromAWARDER','$fromREASON','$fromDEPT',$fromSTATUS')"); } }
$action="";
$msg=1;
}
Assistance much appreciated before I rip my hair out. :)
This may not be the only source of your problems (only the first I notice), but when using <?php $varname ?>, nothing will actually be printed to the screen. Instead you need to use <?php echo $varname; ?>
<td><input name="type[]" type="hidden" id="type" value="Civil">
<input name="awarder[]" type="hidden" id="awarder" value="<?php echo $usersname; ?>">
<input name="datea[]" type="hidden" id="datea" value="<?php echo $stamp83; ?>">
<input name="status[]" type="hidden" id="status" value="Check">
<input name="id[]" type="hidden" id="id" value"<?php echo $memberID; ?>">
<input name="dept[]" type="hidden" id="dept" value"<?php echo $recordTITLE; ?>">
</td>
The A you're seeing inserted is the first letter of Array. When the above values are inserted empty and you try to access them in the for loop with [], the string Array is accessed by character index like an array.
Your INSERT statements, as mentioned in comments, are vulnerable to SQL injection. Escape them with mysql_real_escape_string().
$fromID = mysql_real_escape_string($_POST['id'][$i]);
$fromTYPE = mysql_real_escape_string($_POST['type'][$i]);
$fromCOUNT = mysql_real_escape_string($_POST['count'][$i]);
...
...
$fromSTATUS = mysql_real_escape_string($_POST['status'][$i]);

Categories