Post form with multiple fields with same name attribute - php

I get a information from my DB and then I build a from based on the information. The idea is that I want to capture the score each players made in a game. This obviously mean that I would have an input field with the same name attribute of PID as well as for the score input field. My code looks like this:
<form name="enterscores" id="enterscores" method="post" action="parseFiles/parse_enterscores.php">
<b>Game number: </b> <input type="text" id="entergameID" name="entergameID" maxlength="10" size="10" placeholder="Game number" />
<hr />
<table width="50%">
<tr>
<td>
<b>First name</b>
</td>
<td>
<b>Last name</b>
</td>
<td>
<b>Score</b>
</td>
</tr>
<?php
foreach ($LoginIDs as $PID){
$Login_ID = $PID;
cleanNumber($Login_ID);
foreach (getPlayerDetails($Login_ID) as $playerdDetails) {
$Login_ID = (int)$playerdDetails->Login_ID;
$First_Name = $playerdDetails->First_Name;
$Last_Name = $playerdDetails->Last_Name;
?>
<tr>
<td>
<input type="hidden" name="PID" id="PID" value="<?php echo $Login_ID;?>" />
<?php echo $First_Name; ?>
</td>
<td>
<?php echo $Last_Name; ?>
</td>
<td>
<input type="hidden" id="number_of_players" name="number_of_players" value="<?php echo $number_of_players; ?>" />
<input type="text" id="playerscore" name="playerscore" placeholder="Score" maxlength="3" size="3" />
</td>
</tr>
<?php
}
}
?>
<tr>
<td>
<input type="submit" id="enterscoresbtn" name="enterscoresbtn" value="Submit scores" />
</td>
</tr>
</table>
</form>
<?php
}?>
On my parse page I have the following code:
<?php
if(!$_POST){
header('Location: ../');
exit;
}
if(isset($_POST['enterscoresbtn'])){
require_once (__DIR__ . '/../functions/functions.php');
echo '<pre>';
var_dump($_POST);
echo '</pre>';
$PIDs = $_POST['PID'];
print_r($PIDs);
$playerscores = $_POST['playerscore'];
$number_of_players = $_POST['number_of_players'];
cleanNumber($number_of_players);
// $player = array_combine($PIDs, $playerscores);
//print_r($player);
}?>
The output I get looks like this:
array(5) {
["entergameID"]=>
string(2) "15"
["PID"]=>
string(1) "9"
["number_of_players"]=>
string(1) "4"
["playerscore"]=>
string(3) "104"
["enterscoresbtn"]=>
string(13) "Submit scores"
}
How do I get to post these values and how would I be able to get each login id tie to the correct score on the parse page please?

create your inputs like <input name="playerscore[<?php echo $Login_ID ?>]" /> or something like that. this will give you post data like
$_POST['playerscore'] = array( 23=> 123, 42 => '<somePlayerScore>' /* and so on ... */ )
i would have written this as a comment, but this would not allow formatting the post propperly

Related

Multi input values using foreach and calculating values

<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
when i print using: var_dump($_GET["own_pools"]); I get
array(4) {
[0]=> string(1) "5"
[1]=> string(3) "300"
[2]=> string(3) "280"
[3]=> string(2) "50"
}
the parameters in the link are showing correctly
index?own_pools[]=5&own_pools[]=300&own_pools[]=280&own_pools[]=50&visitor_expect=100000000&ticket_price=15&submit_calc=Calculate
but after that the values did not get the right data back in the form and shown like this:
How can I get it calculated and get the values correctly from the input and returned the entered values back to the input value.
I have searched and read but couldn't find the answer...
I have the following code
<?php
$ticket_price=0;
$visitor_expect=0;
$total_value=0;
$own_pools=0;
$pool_result=0;
if(isset($_GET['submit_calc']) && isset($_GET['own_pools'])) {
$ticket_price = $_GET['ticket_price'];
$visitor_expect =$_GET['visitor_expect'];
$own_pools=$_GET['own_pools'];
if(is_numeric($ticket_price) && is_numeric($visitor_expect)){
// pools calculations
$rev_visitors=((int)$_GET["visitor_expect"]* (int)$_GET["ticket_price"]);
$total_value=($rev_visitors*0.01)*30;
$pool_result = $total_value ;
}
}
?>
<form action="" method="get" >
<table>
<tr>
<?php
// Display headers in one row as colmun
$tds = '';
$sum_main_pools=0;
foreach( $calculations as $index =>$calculation ) {
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
// create TDs for the values
if ($calculation['Calculation']['poolname']<>"Regional Pool"){
$tds .= '<td>
<div class="input text" id="pool_calc">
' . $calculation['Calculation']['total_units'] . '
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
</div>
</td>';
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}
} else {
$tds .= '<td>
' . $calculation['Calculation']['total_units'] . '
</td>';
}
}
var_dump($_GET["own_pools"]);
?>
</tr>
<tr>
<?php
// Printing the values
echo $tds;
?>
</tr>
</table>
<?php
$pool_result = $total_value + $sum_main_pools;
?>
<table>
<tr>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Expected Visitors per day
<input name="visitor_expect" type="text" value="100000000" id="visitor_expect" maxlength="9" size="9" >
</div>
</td>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Ticket Price
<input name="ticket_price" type="text" value="15" id="ticket_price" maxlength="2" size="3" >
</div>
</td>
</tr>
<tr >
<td style="width: 60%">
<div>
<label > <?php echo (isset($pool_result) ? $pool_result : "");?></label>
<input name="submit_calc" type="submit" value="Calculate" id="submit_calc">
</div>
</td>
</tr>
</table>
</form>
Solved it:
added in foreach a key in foreach
foreach( $calculations as $key =>$calculation ) {
and in input value added as array
<input name="own_pools[]" type="number" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'][$key];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}

match multiple words in any order from sql via form submit

Hi I hope someone can help. This is my first attempt at using SQL and I'm finding a challenge, to say the least. What I am trying to do is to write multiple searches for a motorcycle website the customer would enter
make in box 1 model in box 2 product in box 3 year in bod 4
But I stuck trying to match multiple words in any order, customers have to type the exact phrase for it to find the product.
this is the code so far
Thanks for your help in advance
<?php
include 'motorcyclefitmentdb.php';
?>
<center>
<table id="search_box">
<form method="post">
<td><input name="Make" type="text" placeholder="Make....." value="<?php echo isset($_POST['Model']) ? htmlspecialchars($_POST['Make'], ENT_QUOTES) : ''; ?>">
</td>
<td><input name="Model" type="text" placeholder="Model....." value="<?php echo isset($_POST['Model']) ? htmlspecialchars($_POST['Model'], ENT_QUOTES) : ''; ?>">
</td>
<td><input name="Item" type="text" placeholder="Item....." value="<?php echo isset($_POST['Item']) ? htmlspecialchars($_POST['Item'], ENT_QUOTES) : ''; ?>">
</td>
</td>
<td><input name="Year" type="text" placeholder="Year....." value="<?php echo isset($_POST['Year']) ? htmlspecialchars($_POST['Year'], ENT_QUOTES) : ''; ?>"></td>
<td><button type="submit" name="submit search">Submit</button></td>
</form>
</table>
</center>
<br><br>
</div>
<center>
<table class = "customer">
<?php
if(isset($_POST['submit-search'])){
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Item = $_POST['Item'];
$Year = $_POST['Year'];
$sql = "SELECT * FROM `fitment` WHERE `Make` LIKE '%$Make%'
AND `Model` LIKE '%$Model%'
AND `Item` LIKE '%$Item%'
AND `Year Search` LIKE '%$Year%'
";
$stmt = $conn->prepare($sql=presql);
$newsql = presql;
$stmt->bindPram("presql", $newsql, PDO::PARAM_CHAR);
$stmt->execute();
}
if ($stmt->num_rows > 0) {
// output data of each row
while($row = $stmt->fetch_assoc()) {
echo
"
<tr>
</tr><td>
".$row["Image"]."
<br><br>
".$row["Item"]."
<br><br>
SKU: ".$row["SKU"]."
<br>
£ ".$row["Price"]."
<br><br>
".$row["Buy"]."
</td>
<td>
".$row["Make"]." ".$row["Model"]." ".$row["Year"]."
<br><br>
".$row["Engine"]."
</td>
</tr>
";
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
</center>
</div>

How to return to a dynamic page on submit

I'm trying to return to a page that is dynamically created from data stored in a database. However when I try to return to the main dynamic page it does not work. It should on submit redirect to the page that shares the yard id for the track. Instead it just gives me a failure message
Controller
public function update_track($yard_id)
{
$data = array(
'track_id'=>$this->input->post('track_id'),
'train_id'=>$this->input->post('train_id'),
'train_direction'=>$this->input->post('train_direction'),
'train_length' =>$this->input->post('train_length'),
'train_hpt'=>$this->input->post('train_hpt'),
'train_tons'=>$this->input->post('train_tons'),
'train_status'=>$this->input->post('train_status'),
);
$this->load->model('atisyard_model');
$this->atisyard_model->update_track($data);
if($this->db->affected_rows()>0)
{
redirect('/atis/'.$value->yard_id);
}
else
{
echo 'failure';
}
}
View
<?=form_open('atis/update_track/'); foreach ($record as $value) {?>
<h1>Edit Track:</h1>
<b><p>Track Name: </b><?php echo $value->track_no;?></br>
<b>Track Length: </b><?php echo $value->track_length;?>'</br></br></p>
<table cellpadding="5" border="0" align="center">
<input type="hidden" name="track_id" title="track_id" value="<?php echo $value->track_id;?>">
<tr>
<td>Lead Engine No. / Symbol / Work Order:</td>
<td><input type="text" name="train_id" id="train_id" size="28" maxlength="28" value="<?php echo $value->train_id;?>"></td>
</tr>
<tr>
<td>Train Direction:</td>
<td><?=form_dropdown('train_direction', $train_direction, $value->train_direction);?></td>
</tr>
<tr><td>Train Length:</td>
<td><input type="text" name="train_length" id="train_length" size="4" maxlength="6" value="<?php echo $value->train_length;?>"></td>
</tr>
<tr><td>HP/T:</td>
<td><input type="text" name="train_hpt" id="train_hpt" size="2" maxlength="5" value="<?php echo $value->train_hpt;?>"></td>
</tr>
<tr><td>Train Tons:</td>
<td><input type="text" name="train_tons" id="train_tons" size="4" maxlength="6" value="<?php echo $value->train_tons;?>"></td>
</tr>
<td>Train Status:</td>
<td> <?=form_dropdown('train_status', $train_status, $value->train_status);?></td>
</tr>
<tr>
<tr><td><button id="submitbtn" >Submit</button></td></tr>
</table>
Back to Yard
<?php } echo form_close();?>
Taking a second look at your code, would it be better to add the yard_id as a hidden field on your form:
<input type="hidden" name="yard_id" name ="<?=$value->yard_id; ?>" />
then change your redirect to:
redirect('/atis/'.$this->input->post('yard_id'));
If I understand this correctly and you are passing $yard_id into your update_track() function, then shouldn't this:
redirect('/atis/'.$value->yard_id);
Be just :
redirect('/atis/'.$yard_id);
Because I can't see where your getting $value from.

insert not functioning mysql php

i got one problem with this . everytime i go insert it the page just refresh and blank. i dont know where is the problem but i checked my sql the data i use is the same. so i dont know where the problem is.
<form enctype="multipart/form-data" method="POST"> <tr>
<td> Generic Name:<input type="text" class="form-control" name = "gname"></td>
</tr>
<tr>
<td> Brand Name:<input type="text" class="form-control" name = "bname"></td>
</tr>
<tr>
<td> Quantity:<input type="number" min="0" class="form-control" name = "mqty"></td>
</tr>
<tr>
<td> Description:<input type="text" class="form-control" name = "mdesc"></td>
</tr>
<br>
<tr>
<td> <input type="submit" class="btn btn-info" name="add" Value="Add"></td>
</tr>
</form>
<?php
include "../../functions/connect.php";
error_reporting(0);
date_default_timezone_set('Singapore');
$date = date('m/d/Y h:i:s a', time());
?>
<?php
include "../../functions/connect.php";
extract($_POST);
if(isset($add)){
$sql = "INSERT INTO `tbl_meds`(`date`,`generic`,`brand`,`description`,`medqty`) VALUES ('$date','$gname','$bname','$mdesc','$mqty')";
$result = mysql_query($sql) or die("Verification Error: " . mysql_error());
}
?>
EDIT: Result of var_dump($_POST) -
array(5) {
["gname"]=> string(11) "Paracetamol"
["bname"]=> string(8) "Biogesic"
["mqty"]=> string(3) "100"
["mdesc"]=> string(71) "The most prescribed Headache and Fever brand that's Effective and Safe."
["add"]=> string(4) "Save"
}
if(isset($_POST["add"])){
this will work for you
your form has POST method. So on PHP side you have to handle it with $_POST global variable.

assign data from one table to a specific table (group)

I have a classrooms in schools and when I click on a certain classroom, I want to add students into it but my actual code is doing something stupid. It adds a student but i can see the student in all classrooms, not just in the one that i added him into. So when Im in classroom number 1, I see a form in there, I can add a student there, ... see how it works here:
here is the code: http://www.xxxx.xx/projekt/
here is my code in file trieda.php
<table align="center"><tr><td>
<form action="vlozit2.php" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="hidden" name="id_triedy" value="<?= $trieda['id_triedy'] ?>" />
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
</td></tr></table>
<?php
$result = mysqli_query($prip,"SELECT * FROM student ORDER BY meno");
while($student = mysqli_fetch_array($result))
{
echo "<br /><table cellspacing='1' cellpadding='1' class='tabulka1' align='center'><tr>";
echo "<td width='200'><a href='student.php?id_triedy=".$trieda['id_triedy']."".id_student=".$student['id_student']."'>".$student['meno']." ".$student['priezvisko']."</a></td>";
?>
<td width='300px' align='right' bgcolor="#fbfbfb">Zmazať</td>
</tr></table>
<?php
}
?>
here is vlozit2.php (a code that works for the form to add a student)
if(isset($_POST['submit']))
{
//meno a priezvisko
$student = $_POST['meno'];
$student = $_POST['priezvisko'];
$trieda = $_POST['id_triedy'];
//connect to the database
include 'config.php';
//insert results from the form input
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES('$_POST[meno]', '$_POST[priezvisko]', '$_POST[id_triedy]')";
$add = "<table align='center'>
<tr>
<td> Študent bol úspešne pridaný do triedy. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
$not_add = "<table align='center'>
<tr>
<td> Študent s týmto menom a priezviskom už je v tejto triede. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
if (mysqli_query($prip, $sql)) {
echo $add;
}else{
echo $not_add;
}
mysqli_close($prip);
}
?>
Try to replace your part of code with these snipets:
1) in trieda.php
<form action="vlozit2.php?id_triedy=<?php echo $_GET["id_triedy"];?>" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
2) in vlozit2.php
$student = $_POST['meno'];
$priezvisko = $_POST['priezvisko'];
$id_trieda = $_GET['id_triedy'];
and
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES( '{$student}', '{$priezvisko}', {$id_trieda} )";
Hopefully you store your id_trieda as INT type.
In your vlozit2.php file is nothing about inserting of class id. So put
<input type="hidden" name="classId" value="<?= $trieda['id'] ?>" />
to your form and in vlozit2.php get this value from $_POST['classId'] and insert it with other students data or anywhere you want to have it.

Categories