How to submit an HTML form into a PHP array - php

I've looked through some posts around here, but am still struggling to get my code to work correctly....
I've got a form that is being dynamically created so I never know how many rows it will have, the number of columns is static though. Here is my current code...
echo '<form name = "confirmPlayers" enctype="multipart/form-data" action="page.php" method="POST">';
echo '<input type="hidden" name="confirm" value="1"/>';
echo '<input type="hidden" name="pg_function" value="match"/>';
echo '<table border = "1">';
echo '<tr> <td colspan = "7"> <b>Matched Members</b></td> </tr>';
echo '<tr> <td> Match </td> <td>Division</td> <td>Number</td> <td>Upload Number</td> <td>KDGA Name</td> <td>Upload Name</td> <td>Score</td></tr>';
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[]" value="1" checked>';
echo '<td> ' . $row['division'] . ' </td>';
echo '<td> ' . $row['mplayer_number'] . ' </td>';
echo '<td> ' . $row['tplayer_number'] . ' </td>';
echo '<td> ' . $row['mfirst_name'] . ' ' . $row['mlast_name'] . ' </td>';
echo '<td> ' . $row['tfirst_name'] . ' ' . $row['tlast_name'] . ' </td>';
echo '<td> ' . $row['score'] . ' </td>';
echo "</tr>";
}
echo '<tr> <td>';
echo '<input type="submit" value="Submit" />';
echo '</td> </tr>';
echo '</table> </form>';
I've tried several variations on creating the array but just seem to be creating a mess. I basically need all of the information from each cell returned in order to pass on to the database for further processing. I was hoping to return an array with a row for each corresponding row in the generated table, and then keys/indexes for each column displayed. Right now if I use a print_r($_POST['match_chkBox']); then it is returning correctly with the number of rows I left checked before submitting, but any other changes.. not so pretty.
Would appreciate any help you can give... this is driving me nuts!

try giving value unique id
echo '<td> <input type="checkbox" name="match_chkBox[]" value=value=\"".$row['id']."\"checked>';

if i were you i will post $row[id] (as defined by angel) only and fetch respective data at my php code from the database.
but if you want to take all data with you you can simply do the following..
<pre>
$i=0;
while($row = mysql_fetch_array($result)){
$i++;
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[$i]" value="'.$row['id'].'" checked>';
echo '<td> ' . $row['division'] . ' <input type="hidden" name="division['.$i.']"> </td>';
echo '<td> ' . $row['mplayer_number'] . ' <input type="hidden" name="mplayer_number['.$i.']"> </td>';
// and so on....
echo "</tr>";
}
</pre>
in since html form sent only checked checkbox to the server you can have the following code to check user selections
<pre>
foreach($_POST['match_chkBox'] as $k=>$value) {
$divi = division[$k];
$mplay = mplayer_number[$k];
//now insert into your database.
}
</pre>
hope this will help

Set the value of the checkbox to the unique key of that row, there by when the form is posted, match_chkBox[] will contain the unique keys.
echo"<input type='checkbox' name='match_chkBox[]' value=\"$row[unique_id]\" />";
Use view source to see if each rows value is set to the unique_id of that row

Related

How to get value from input with data from database

I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}

Php array looping, creating dynamic variable for purchasing form

Still getting used to stackoverflow excuse my rookie-ness.. :)
Have an SQL query that returns a data put into a table in php. I want this table to be used for purchasing.
My idea was to use the product id, meaning i would use a dynamic php variable (not sure if I'm doing that right now) believe I saw a post something like $.$varaible$.$ it wasn't very clear and was a different subject.
My code is as follows:
$result = mysqli_query($con, "SELECT * FROM Product WHERE Type = 'Game'");
?>
<div class="wrapper">
<h1 class="headGame">Buy Some Games Man</h1>
</div>
<br />
<div class="wrapper">
<?php
echo
"<table border='1'>
<tr>
<th> Name </th>
<th> Picture </th>
<th> Console </th>
<th> Description </th>
<th> Price </th>
<th> Amount </th>
</tr>";
echo '<form id="gamesOrder" action="purchase.php">';
while($row = mysqli_fetch_array($result)) {
$id = $row['Pd_Key'];
echo"<tr>";
echo"<td>" . $row['Name'] . "</td>";
echo"<td>" . '<img class="prdPic" src="'. $row['Picture']. '">' . "</td>";
echo"<td>" . $row['Type2'] . "</td>";
echo"<td>" . $row['Description'] . "</td>";
echo"<td>" . $row['Price'] . "</td>";
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
echo"</tr>";
}
echo '<input type="submit" value=" BUY ">';
echo '</form>';
?>
When I click the submit it changes the url but nothing happens, it doesn't redirect.
Any advice on how to get this entire process to work. The variable being used in a purchasing form, via a php file ie (purchase.php) and the variable to use for this.
EDIT - Had minor errors, but still not 100% on variable %id, won't that get redefined each loop, how can I have it dynamic so it can be used in a form to identify what the user wants to buy.
Now redirects but not to purchase.php
URL is ~/purchase.php?1=0&2=0&3=0&4=0&5=0&6=0&7=0&8=0&9=0&10=0&11=0&12=0&13=0&14=0&15=0&16=0&17=0&18=0&19=0&20=0&21=0
Thanks you legends you!! =D
You are missing the closing form caret:
echo '<form id="gamesOrder" action"purchase.php"';
should be:
echo '<form id="gamesOrder" action="purchase.php">';
Also you concatenation for ID is incorrect:
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should just be:
echo"<td>" . '<input type="number" min="0" max="100"; name="id" value="' .$id. '" value=0></td>';
To access the id in purchase.php use the following code:
$id = isset($_GET['id']) ? $_GET['id'] : null;
And you need to assign the action to the form with an equals sign:
action="myaction.php"
And you don't pass the id right...
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should be
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
Ough, and on form you need to put action =
echo '<form id="gamesOrder" action="purchase.php">';
Ok, first you've got some HTML error, lets see:
In the Form element you need to close it and also include the method (as POST), see:
< form id="gamesOrder" action="purchase.php" method="POST">
To send data using a form you will need to include the data from the data base in form fields like this:
echo '< input type="text" name="myFieldName" value="'.$row['Price'].'">';
Any question let me know...
Cheers.

I need to upload an image with PHP and insert file names in database

I have a PHP form that updates records in a database. It looks something like this.
//update a record
$query1 = 'UPDATE mytable SET name="'.$name.'", description="'.$desc.'",
img="'.$img.'" WHERE id="'.$id.'" ';
mysqli_query($con,$query);
//get record set
$query2 = 'SELECT * FROM mytable WHERE id="'$id'"';
$result = mysqli_query($con,$query2);
echo '<form action="my-update-page.php" method="post">';
//table heading row
echo '<table width="1000" border="1" cellspacing="0" cellpadding="1">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>NAME</td>';
echo '<td>description</td>';
echo '<td>Image</td>';
echo '</tr>';
//display data
while($row = mysqli_fetch_array($result))
{
echo '<input type="hidden" name="id" value="' . $row['id'] . '" />';
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td><input type="text" name="name" value="'. $row['name'].'" /></td>';
echo '<td><textarea name="description">'.$row['description'].'</textarea></td>';
echo '<td><input type="text" size="3" name="img" value="'. $row['img'].'"/>;
echo 'Upload Image</td>';
echo '</tr>';
}
//closing tag for table
echo '</table>';
echo '<br /><input type="submit" value="submit" /></form>';
I want my upload.php page to open in a popup where the user can upload the image. I'm pretty sure I can manage doing that. Where I get stuck is after the file is uploaded, I want the popup to close and file name to show in the form input.
Modify to reflect your names, but window.opener is the link to the other window. At that point, access any elements the same way.
window.opener.forms['myform'].elements['fileinput'].value = nameOfFile;
window.close();
As the comment above says, use AJAX to do the file post.

One Form, Two Buttons To Send Info to Different Pages

I have a table being generated from PostgreSQL through PHP.
The user needs to have the option of deleting the row or updating it.
So I have successfully implemented a delete button utilizing hidden elements to pass the key information for the deletion to my delete.php
Now I am trying to add an update button which will send the php to my update.php rather than my delete.php.
However, when I open my form I tell it where to send the data upon submission.
My question then is, is there a way that I can have both buttons available and depending on which button is pressed the form posts the data to the appropriate php- delete.php vs update.php? Perhaps some attribute of input buttons I cannot think of that can direct where to POST rather than having it as a form attribute?
Here is an example of what I have:
echo '<form action="delete.php" method="POST">';
echo '<tr>';
echo '<td class="even">' . $row['id'] . '</td>';
echo '<td class="odd">' . $row['name'] . '</td>';
echo '<td class="even">' . $row['countrycode'] . '</td>';
echo '<td class="odd">' . $row['district'] . '</td>';
echo '<td class="even">' . $row['population'] . '</td>';
echo '<input type="hidden" name="todelete" value="'.$row['countrycode'].'" />';
echo '<input type="hidden" name="cityname" value='.$row['name'].'" />';
echo '<input type="hidden" name="tablename" value="'.$_POST["search-type"].'" />';
echo '<td> <input class="odd" type="submit" name="updateBtn" value="Update?" /></td>';
echo '<td> <input class="even" type="submit" name="deleteBtn" value="Delete?" /></td>';
echo '</tr>';
echo '</form>';
This is generated for each row of the table so each of the forms is unique and only that data is sent.
Let me know if you have any ideas how I can implement the two buttons!
Simplest method is to have a SINGLE script as your action, then have a basic
<?php
if (isset($_POST['deleteBtn'])) {
... do delete stuff
} else if (isset($_POST['updateBtn'])) {
... update stuff here ...
}
Otherwise you're stuck with some javascript to dynamically change the action target.

Values from dynamically produced checkboxes

I am trying to get the values of a dynamically created set of checkboxes in PHP but apparently I couldn't get it. The source codes are below.
The "managestaff.php" page would allow searching for staff via their names and throws out a list of names with checkboxes for the admin to check them and click on a "delete" button at the bottom to delete the staff whom are being checked.
The deletion would be done on "deletestaff.php" as the "delete" button on "managestaff.php" simply forwards these values to "deletestaff.php" to do deletion work of the staff.
"managestaff.php" page codes:
<b><h3>Manage Staff</h3></b><br/>
<form action="managestaff.php" method="POST">
<input name="form" type="hidden" id="form" value="true">
<table width=300>
<tr>
<td width=112>Staff Name: </td>
<td width=188><input type="text" class="textfield" name="sname" /><br/></td>
</tr>
</table><br/>
<input type="submit" value="submit" />
</form>
<?php
if (isset($_POST['form']) && (isset($_POST['sname'])) && $_POST['form'] == 'true') {
$space = ' ';
$staffname = mysql_real_escape_string($_POST['sname']);
$query = 'SELECT * from staff where staffname like \'%' . $staffname . '%\'';
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo '<br><br>';
echo '<table>';
echo '<tr><th>Staff ID' . $space . '</th><th>Staff Name' . $space . '</th></tr>';
echo '<form action="deletestaff.php" method="POST">';
echo '<input name="delstaffform" type="hidden">';
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['staffid'] . '</td><td>' . $row['staffname'] . '</td>';
// :Begin - dynamic checkbox generation for deleting staff
echo '<td>';
echo '<input type="checkbox" name="delstaff" value="' . $row['staffid'] . '" />';
echo '</td>';
// :End
echo '</tr>';
}
echo '<tr align="right"><td colspan="3"><input type="submit" value="delete"/></td></tr>';
echo '</form>';
echo '</table>';
}
}
?>
"deletestaff.php" page codes:
<?php
print_r('POST: ' . $_POST);
echo '<br>';
if (isset($_POST['delstaffform']) && isset($HTTP_POST_VARS)) {
echo 'Submission of delstaffform FOUND !';
echo 'Staff to delete' . $HTTP_POST_VARS['delstaff'];
}
else{
echo 'Submission of delstaffform NOT FOUND !';
}
?>
The "deletestaff.php" doesn't do delete for now as it's a test page.
The current output I get is "Submission of delstaffform NOT FOUND !".
Thanks for the solutions.
Try this:
<input type="checkbox" name="delstaff[]" value="' . $row['staffid'] . '"/>';
print_r your $_POST and you'll see it sticks your submissions nicely into an array for you.
<?php
if (isset($_POST['delstaff']) && is_array($_POST['delstaff'])) {
echo 'Submission of delstaffform FOUND !';
$array = $_POST["delstaff"];
foreach($array as $value){
echo "<br>Value: ".$value."<br>";
}
} else {
echo 'Submission of delstaffform NOT FOUND !';
}
?>
Found the answer on my own but nevertheless you are helpful :D . Thanks a lot.

Categories