Giving different names to a html object in a loop in PHP - php

How would I be able to give the different things different names (as I am creating them in a loop) so that I can use the data? Could I modify name=\"type\" to maybe include $x within the name so that it is different each time?
For($x=0; $x<=$noQuestions-1; $x++){
echo "<table>";
echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
echo "<form method=\"post\">";
echo "<Select class=\"form-control\" type=\"text\" name=\"type\" required>";
echo "<option value=\"1\">".$optionData[$x][0]."</option>";
echo "<option value=\"2\">".$optionData[$x][1]."</option>";
echo "<option value=\"3\">".$optionData[$x][2]."</option>";
echo "<option value=\"4\">".$optionData[$x][3]."</option>";
echo "</select>";
echo "</form>";
echo "</table>";
}

You can simply concatenate variable $x in name like this:
echo "<Select class=\"form-control\" type=\"text\" name=\"type".$x."\" required>";
EDIT:
$type = "dropdown";
for($x=0; $x<=$noQuestions-1; $x++)
{
echo "<table>";
echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
echo "<form method=\"post\">";
echo "<Select class=\"form-control\" type=\"text\" name=\"".$type.$x."\" required>";
echo "<option value=\"1\">".$optionData[$x][0]."</option>";
echo "<option value=\"2\">".$optionData[$x][1]."</option>";
echo "<option value=\"3\">".$optionData[$x][2]."</option>";
echo "<option value=\"4\">".$optionData[$x][3]."</option>";
echo "</select>"; echo "</form>";
echo "</table>";
}

One simple solution would be to add $x in the name like this for example:
echo "< select class=\"form-control\" type=\"text\" name=\"type-$x\" required>";

Related

PHP $_POST variables between functions in a class

I can get the $_POST variables in the next function once, but when repeating the function or going to the next function the variables aren't accessible. I am using PHP class and multiple functions. I have searched and haven't found the solution. Maybe I am missing something simple. I am new to using classes in PHP, and also $_POST variables. I am unclear why they aren't accessible within some functions. Thanks in advance.
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$MainPage = new MainPage();
$MainPage->showMainPage();
class MainPage
{
function __construct()
{
}
public function showMainPage()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "<meta charset=\"utf-8\">";
echo " </head>";
echo "<body>";
$showmainoptions = 0;
if (isset($_GET['showmainoptions']))
{
$showmainoptions = $_GET['showmainoptions'];
}
$showFinOptions = 0;
if (isset($_GET['showfinoptions']))
{
$showFinOptions = $_GET['showfinoptions'];
}
$showInspectionList = 0;
if (isset($_GET['showinspectionlist']))
{
$showInspectionList = $_GET['showinspectionlist'];
}
if($showmainoptions)
{
$this->showMainOptions();
}
if($showFinOptions)
{
$this->showFinOptions();
}
if($showInspectionList)
{
$this->showInspectionList();
}
$this->showFooterHTML();
return;
}
public function showMainOptions()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
echo "Select Main Options<br>";
echo "<form name=\"submitMainOptions\" id=\"submitMainOptions\" method=\"post\" action=\"mainpage.php?showmainoptions=0&showfinoptions=1&showinspectionlist=0&addnewinspection=0\">";
echo "<label><font size=4px>Wharehouse</font></label> ";
echo "<select id=\"house\" name=\"house\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"CYP\">Cypress</option>";
echo "<option value=\"FAY\">Fayetville</option>";
echo "<option value=\"FUR\">Furnace</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Fin Type</font></label> ";
echo "<select id=\"fintype\" name=\"fintype\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"Copper\">Copper</option>";
echo "<option value=\"Aluminum\">Aluminum</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Shift</font></label> ";
echo "<select id=\"shift\" name=\"shift\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"1\">Shift 1</option>";
echo "<option value=\"2\">Shift 2</option>";
echo "<option value=\"3\">Shift 3</option>";
echo "</select>";
echo "<br>";
echo "<button type=\"submit\" class=\"btn btn-primary btn-block btn-flat\">Continue</button></a>";
echo "</form>";
}
public function showFinOptions()
{
$house = $_POST['house'];
echo $house."<br>";
echo "Select Fin Options<br>";
echo "<form name=\"submitFinSelection\" id=\"submitFinSelection\" method=\"post\" action=\"mainpage.php?showmainoptions=0&showfinoptions=0&showinspectionlist=1&addnewinspection=0\">";
echo "<label><font size=4px>Part No</font></label> ";
echo "<select id=\"partno\" name=\"partno\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"partno1\">partno 1</option>";
echo "<option value=\"partno2\">partno 2</option>";
echo "<option value=\"partno3\">partno 3</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Fin Size</font></label> ";
echo "<select id=\"finsize\" name=\"finsize\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"5mm\">5mm</option>";
echo "<option value=\"7mm\">7mm</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Press No</font></label> ";
echo "<select id=\"pressno\" name=\"pressno\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"CF016\">CF016</option>";
echo "<option value=\"CF018\">CF018</option>";
echo "<option value=\"CF020\">CF020</option>";
echo "</select>";
echo "<br>";
echo "<button type=\"submit\" class=\"btn btn-primary btn-block btn-flat\">Continue</button></a>";
echo "</form>";
}
public function showInspectionList()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$house = $_POST['house'];
echo $house."<br>";
$pressno = $_POST['pressno'];
echo $pressno."<br>";
}
public function showFooterHTML()
{
echo " </body>";
echo "</html>";
return;
}
}
?>

PHP Basket quantity variable

I am trying to create a php page where the materials from database are populated. Users should be able to enter the quantity next to the item they wish to order and I have created a qty text field for this
<?php
session_start();
include("db.php");
$pagename="Order Material";
echo "<html>";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
include ("detectlogin.php");
echo "<link rel=stylesheet type=text/css href=mystylesheet.css>";
$sql="select * from material";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type=text name=qty value=qty size=5></td>";
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
?>
However, I cannot successfully post the value of qty on to the next page even though I have $qty=$_POST['qty']; on my request_material.php. Do you know why this value entered in the qty field cannot be posted onto the request_material.php page? Do I need a session variable?
thanks
Because input tag name="qty" is outside the form tag
echo "<td><input type=text name=qty value=qty size=5></td>";// outside form tag
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=" . $arraymaterials['materialCode'] . ">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
You need to add it inside your form tag as
echo "<form action=request_material.php method=post>";
echo "<td><input type=text name=qty value=qty size=5></td>";// add inside it
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
Please try this: I have updated the code:
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=request_material.php method=post>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "<input type=hidden name='materialcode[]' value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</tr>";
}
echo "</form>";
}
echo "</table>";
In request_material.php check value of $qty.It will be an array.for more details print_r($_POST) in request_material.php

Prevent double options in dropdown list

in my dropdown list I insert a value from a mySQL query. This Value is my selected Option.
The problem is, that he duplicate my selected Value (it is logical because I create "6 Options" but the user only should have "5 Options").
How can I prevent this?
This is my Code:
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
echo "<option value='".$row['a_qty']."' selected>".$row['a_qty']."</option> ";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "</select>";
echo "<script>
And my Output:
Try as below :
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
for($i=1;$i<=5;$i++)
{
$selected = '';
if($i==$row['a_qty'])
$selected = 'selected="selected"';
echo "<option value='$i' $selected>$i</option>";
}
echo "</select>";

track change in an array then submit the array to the same page

I have the following php code (I omitted the irrelevant part of code):
<?php
echo "<form action=$_SERVER[PHP_SELF] method=\"POST\">";
echo "<table>";
for ($i=0;$i<$num_rows;$i+=1) {
echo "<tr>";
echo "<td align=center>$variable1[$i]</td>";
echo "<td align=center>$variable2[$i]</td>";
echo "<td align=center><input type=\"text\" name=\"variable3[$i]\" value=\"$variable3[$i]\" /></td></tr>";
echo "<input type=\"hidden\" name=\"variable1[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable2[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable3[$i]\" value=\"$variable1[$i]\">";
}
echo "<tr><td colspan=\"3\"><input name=\"action\" type=\"submit\" value=\"Update\"></td></tr>";
echo "</table>";
echo "</form>";
?>
I want to pass the variables through POST method to the same page. The code retrieves data from the database and lists it in a table. However, I want to be able to manually modify variable3 and submit the array after I do this. The problem is that the array is submitted before I modify the variable3. How can I submit the array with the value of variable3 modified? How can I check if variable3 was modified and do the submitting afterwards? Thanks.
delete line 11 like this
<?php
echo "<form action=$_SERVER[PHP_SELF] method=\"POST\">";
echo "<table>";
for ($i=0;$i<$num_rows;$i+=1) {
echo "<tr>";
echo "<td align=center>$variable1[$i]</td>";
echo "<td align=center>$variable2[$i]</td>";
echo "<td align=center><input type=\"text\" name=\"variable3[$i]\" value=\"$variable3[$i]\" /></td></tr>";
echo "<input type=\"hidden\" name=\"variable1[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable2[$i]\" value=\"$variable1[$i]\">";
}
echo "<tr><td colspan=\"3\"><input name=\"action\" type=\"submit\" value=\"Update\"></td></tr>";
echo "</table>";
echo "</form>";
?>
Now you can see if it has been changed by cheking the value of $_POST['variable3'].

php included form acting strangley with Firefox

For some reason when I call a function to create a form, the drop down menus aren't sticky and the browser forces users to click into the first text field and tab through the rest. It won't let them mouse through the fields. This is only happening in FF, not IE or Chrome. The forms I'm including are just basic html and only php pages I include are doing this.
Here is one function:
function addNoteUI($keyword) {
echo "<div id='search_result_right'>";
echo "<center><div id='enter_note_header'>Assign a Salesperson</div></center><p>";
echo "<form id='response' action='notes_add.php' method='post'>";
echo "<label for='mod_num'>MOD Initials: <label>";
echo "<input type='text' name='mod_num' size='2' maxlength='4'><p>";
echo "<label for='sales_num'>Assigned to Sales Person: <label>";
echo "<input type='text' name='sales_num' size='2' maxlength='4'><p>";
echo "<input type='hidden' name='question_num' value='$keyword'>";
echo "<label for='response'>Note</label><br>";
echo "<textarea name='response' cols='30' rows='7 maxlength='510'></textarea><p>";
echo "<input type='submit' value='Assign'>";
echo "</form>";
echo "</div>";
Here is the other:
function changeDept() {
include 'ask_search.php';
echo "<div id='search_result'>";
echo "<form action='change_dept.php' method='post'>";
echo "<label for='current_num'>Enter the Question Number to be Changed: <label>";
echo "<input type='text' name='current_num' size='4'><p>";
echo "<label for='store'>Select New Store/Department: <label>";
echo "<select name='store'>";
echo "<option>Please Select</option>";
echo "<option value='Albany'>Sales (Albany Store)</option>";
echo "<option value='Saratoga'>Sales (Saratoga Store)</option>";
echo "<option value='Web Sales'>Sales (TaftFurniture.com)</option>";
echo "<option value='Financing'>Financing</option>";
echo "<option value='Customer Service'>Customer Service</option>";
echo "<option value='Delivery'>Delivery</option>";
echo "<option value='HR'>Human Resources</option>";
echo "<option value='Web Contact'>Website Comment</option>";
echo "<input type='submit' value='Change' id='dropdown'>";
echo "</select></form></div>";
}
Thanks in advance.
Your labels are not closing properly:
echo "<label for='mod_num'>MOD Initials: <label>";
Should be:
echo "<label for='mod_num'>MOD Initials: </label>";
Also, in the second example, you have an input inside the select. The input must be outside:
echo "<option value='Web Contact'>Website Comment</option>";
echo "<input type='submit' value='Change' id='dropdown'>";
echo "</select></form></div>";
Should be:
echo "<option value='Web Contact'>Website Comment</option>";
echo "</select>";
echo "<input type='submit' value='Change' id='dropdown'></form></div>";
And another one, you're not closing your P tags:
echo "<input type='text' name='mod_num' size='2' maxlength='4'><p>";
Should be:
echo "<p><input type='text' name='mod_num' size='2' maxlength='4'></p>";
Try to be more careful with your tags. Some browsers are more forgiving about malformed HTML, but others are not.

Categories