different array for different input text - php

hi i want to store value of each input text in different arrays how to do that
for example store value of input text 1 in array 1 and value of input text 2 in array 2 and so on how to achieve that
here is the code for print input text
for($r=1;$r<=10;$r++)
{
echo"<form id='ponts'>
<table>
<tr>
<td>Enter point number$r</td><td> <input type='text' id='pt$r' name='pt$r' pattern='[0-9.]+'/></td>
</tr>
</table>
</form>";
}

I guess I didn't understand well, but the following script may be what you want.
<?php
$g=$_GET;
if( isset($g['pt']) ){
// the form has been submitted.
$ptValues=$g['pt'];
print_r($ptValues);
}
echo "<form id='ponts'><table>";
for($r=1;$r<=10;$r++)
{
echo "<tr><td> Enter point number$r</td><td> <input type='text' id='pt$r' name='pt[]' pattern='[0-9.]+'/> </td></tr>";
}
echo "</table></form>";
?>
Maybe this:
<?php
$g=$_GET;
if( isset($g['pt0']) ){
// the form has been submitted.
$ptValues=array();
for($i=0; isset($g['pt'.$i]); $i++ )
$ptValues[]=$g['pt'.$i];
print_r($ptValues);
}
echo "<form id='ponts'><table>'";
for($r=1;$r<=10;$r++)
{
echo "<tr><td> Enter point number$r</td><td> <input type='text' id='pt$r' name='pt$r' pattern='[0-9.]+'/> </td></tr>";
}
echo "</table></form>";
?>

maybe this is what u are looking for
echo"<table id='points'>";
for($r=1;$r<=10;$r++)
{
echo"
<tr>
<td>Enter point number".$r."</td><td> <input type='text' id='pt".$r."' name='pt".$r."' pattern='[0-9.]+'/></td>
</tr>
";
}
echo "</table>";
i dont know why you are using the form tags here .

Related

Refuses to get data

I am using this code to get the value of checked box in PHP but it doesn't work in the second code. The first one is just a list for testing. The second one will echo data from a database:
When applying the first code on the test list I will get the value of the box I checked but when applying it on the second part (one that gets data from database it returns Empty)
Get the checked boxes:
<?php
if(isset($_POST['DeleteCon']) )
{
if(!empty($_POST['lang']))
{
foreach($_POST['lang'] as $value)
{
echo "value : ".$value.'<br/>';
}
}
else
{
echo "value : Empty <br/>";
}
}
?>
Test list: it works on this section of the code:
<form method="post">
<?php
echo "<span>Select languages</span><br/>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'> PHP <br/>
<input type='checkbox' name='lang[]' value='JavaScript'> JavaScript <br/>
<input type='checkbox' name='lang[]' value='jQuery'> jQuery <br/>
<input type='checkbox' name='lang[]' value='Angular JS'> Angular JS <br/>"
?>
</form>
But doesn't work on this one:
<form method="post">
<?php
include('database.php');
$sql = "SELECT id, ContactID ,FirstName, LastName, Phone FROM Contact WHERE ID='1'";
$result = $conn->query($sql);
if ( !empty($result->num_rows) && $result->num_rows > 0) { // ...
// output data of each row
echo "<form method='post'>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td id='delete'>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'>
</td>
<td>". $row["FirstName"]. "</td>
<td>". $row["LastName"]. "</td>
<td>". $row["Phone"] ."</td></tr>";
}
echo "</form>";
} else {
echo "<tr>
<td id='delete'>
<input type='checkbox' id='row1' class='table-row'>
</td>
<td> 0 results </tr>";
}
$conn->close();
?>
</form>

how to send a selected database value that is inside loop to another page in php

i have made a form in php and inside form there is a loop that extracts multiple values from database(img,name,availability,..) for multiple books i have made a table to display those values to user and after displaying these data in table i have made an issue button inside loop so that every book has its issue button.
My problem is that i have to only retrieve id of that book for which user click issue button. i tried storing it in cookie but it send the id of the first book displayed then i tried get method but that results in sending the last book that is displayed on screen id. but i want is that it should send the id of book which is selected by user
display books
echo "<form action='issue.php' method='get'>";
while ($row= mysqli_fetch_array($result)) {
echo "<div id='img_div' style='background-color:#fff;'> ";
echo "<img src='books/".$row['image']."'>";
// echo "</div>";<div id='text'>
$isbn=$row['isbn'];
echo "<input type='hidden' name='isb' value='$isbn' />";
echo " <table>";
echo "<tr><td> NAME</td><td> ".$row['name']."</td></tr>";
echo "<tr><td> AVAILABILITY</td><td> ".$row['availabilty']."</td></tr>";
echo "<tr><td> CATEGORY</td><td> ".$row['category']."</td></tr>";
echo "<tr><td colspan='2'>
<button type='submit' name='issue'>issue</button></td></tr>";
echo "</table>";
echo "</div><br/>";
if (isset($_GET['issue'])) {
# code...
$bookid=$isbn;
setcookie("bid",$bookid);
if(!isset($_COOKIE['bid'])){
echo "COOKIE NOT SET";
}
else{
echo "COOKIE SET SUCCESSFULLY";
}
}
issue.php(in which i want to send id)
if(isset($_GET['issue'])){
$bookid=$_GET['isb'];
$dbser="localhost";
$use="[redacted]";
$pasw="[redacted]";
$db="[redacted]";
$con=mysqli_connect($dbser,$use,$pasw,$db);
mysqli_select_db($con,$db)or die("db not connected");
$userid=$_COOKIE['id'];
$id=$_SESSION['user']['username'];
$query = "select id from user_account where username='$id'";
$result=mysqli_query($con,$query);
$row= mysqli_fetch_assoc($result);
$uid=$row['id'];
echo "$uid";
echo "<br/>";
echo "$bookid";
$query = "INSERT INTO issue (bookid, userid)
VALUES ('$bookid', '$uid')";
mysqli_query($con, $query)or die(mysqli_error($con));
What you need is a way to select a single row then submit that with the form. That can be accomplished by adding a Radio button to your table inside the form. The user will check the radio button for the item they want then click the submit button.
Here is an example of what that code could look like for your page.
display books
<form action='issue.php' method='get'>
while ($row= mysqli_fetch_array($result)) {
$isbn=$row['isbn'];
echo " <table>";
echo "<tr>";
echo "<td><input type=\"radio\" name=\"optradio\" value=\"".$isbn."\"></td>";
echo "<td> NAME</td><td> ".$row['name']."</td>";
echo "<td> AVAILABILITY</td><td> ".$row['availabilty']."</td>";
echo "<td> CATEGORY</td><td> ".$row['category']."</td>";
echo "</tr>";
echo "</table>";
}
echo "<button type='submit' name='issue'>issue</button>";
echo "</form>";
Here is a HTML snippet so you can see what that PHP code would output in HTML. Click "Run snippet code" below to see the preview.
<form action='issue.php' method='get'>
<table>
<tr>
<td><input type="radio" name="optradio" value="1"></td>
<td> NAME</td>
<td> name1</td>
<td> AVAILABILITY</td>
<td> availability1</td>
<td> CATEGORY</td>
<td> category1</td>
</tr>
<tr>
<td><input type="radio" name="optradio" value="2"></td>
<td> NAME</td>
<td> name2</td>
<td> AVAILABILITY</td>
<td> availability2</td>
<td> CATEGORY</td>
<td> category2</td>
</tr>
</table>
<button type='submit' name='issue'>issue</button>
</form>
Then in issue.php you would look for $_GET['optradio'] to get the selected value.
if(isset($_GET['issue'])){
$bookid=$_GET['optradio'];
$dbser="localhost";
...
...
...

Php form array not passing values to other pages

I am working on a project where I want users to choose their space for parking and book it. I have a MySQL database which holds information about parking slots. I am fetching those values and display it using table and form. I have put checkboxes to make choice. and once they make choice they should be directed to payment page.
I am having problem with checkbox. I can see that in value fields it has values from database but when I hit submit button it doesn't pass any values to next page.
below is my code
<body>
<form method="POST" action="book.php">
<?php
//we create a table
echo "<table>";
// create table th
echo "<tr > <th> Parking Slot No </th> <th> Status </th>";
$sql=" select ParkingSlotNo,Status from fleming_dwing ";
$st=$conn->prepare($sql);
$st->execute();
$total=$st->rowCount();//get the number of rows returned
if($total < 1 ){//if no row was returned
echo "<tr> <td style> No Data: DataBase Empty </td> ";//print out error message
echo "<td> No Data: DataBase Empty </td> ";//print out error message
$ing = "<img src='img/occupied.png'/>" ;
}
else{
while($res = $st->fetchObject()){//loop through the returned rows
echo "<tr>";
if($res->ParkingSlotNo and $res->Status=='OCCUPIED')
{echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/occupied.png'/> </td>";
echo"<td><a href=''> </a> </td>";
}
else
{
echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/vacant.png'> </td>";
echo"<td><input type='checkbox' value='$res->ParkingSlotNo'></td>";
}
echo"</tr>";
}
}
?>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is the code for booking page
<?php
require_once("dbconfigpdo.php");
print_r($_POST);
?>
The checkboxes do not have name attributes. A form control can't be successful (included in the name=value pairs of data that are submitted) without one.
Any input must have a name attribute.
By name you can use a value. So, you need to add name="your name" to your checkboxes.
You need to set an attribute name to your input field, or it will not be processed.
Something like:
<input name='parkingslot' type='checkbox' value='$res->ParkingSlotNo'>

How to use checkbox to delete data from database after submit [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I am making this website and i got some data in my database. The data is read automatically with php into a table. Every table row also gets automatically a checkbox. Now I want to check the checkbox and then press delete and it deletes the checked data out of the database. Any ideas how?
Here is my code:
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Username</th>
<th>Paswoord</th>
<th>Delete</th>
</tr>
<?php
while($user = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$user['UserName']."</td>";
echo "<td>".$user['Pass']."</td>";
echo "<td> <input type='checkbox' name='checkbox' value='checked'</td>";
echo "</tr>";
}
?>
</table>
<form method= "POST" action="deleteuser.php">
<input type="submit" name="delete" value="DELETE USERS">
</form>
this is the code in my delete user.php
$checked = $_POST['checkbox'];
if($_POST['checkbox'] == "checked"){
echo "SUCCEEEEEEES";
}
There are couple of things to be aware of;
The syntax and proper way of writing the <form> tags.
Use the <input> fields inside the <form>.
Pass an array of checkbox[] instead of a single checkbox.
Your Code
<form method= "POST" action="deleteuser.php">
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Username</th>
<th>Paswoord</th>
<th>Delete</th>
</tr>
<?php
while($user = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$user['UserName']."</td>";
echo "<td>".$user['Pass']."</td>";
echo "<td> <input type='checkbox' name='checkbox[]' value='checked'</td>";
echo "</tr>";
}
?>
</table>
<input type="submit" name="delete" value="DELETE USERS">
</form>
PHP
Instead of using this;
if($_POST['checkbox'] == "checked"){
echo "SUCCEEEEEEES";
}
?>
Use this;
<?php
foreach($_POST['checkbox'] as $val)
{
echo $val . " this should be deleted";
}
?>
Well this is what I always do :-
Make these changes to your while loop.
<?php
$c=0;
while($user = mysql_fetch_assoc($records)){
$c++
echo "<tr>";
echo "<td>".$user['UserName']."</td>";
echo "<td>".$user['Pass']."</td>";
echo "<td> <input type='checkbox' name='checkbox-$c' value='checked'</td>";
echo "</tr>";
}
echo "<input type='hidden' name='total' value='$c'>";
?>
deleteuser.php (Access all the checkboxes here via a for loop)
<?php
for($i=1;$i<=$_POST['total'];$i++)
{
if($_POST['checkbox-$i'] == "checked")
{
//commands for delete
}
}
?>
Rename the name of the checkbox from checkbox to checkbox[]. Now you get an Array instead a string.
And then set value-attribute to the ID of you row in the table (Primary Key or whatelse you need to delete them)
echo "<td> <input type='checkbox' name='checkbox[]' value='".$user["UserID"]."'</td>";
Now you can iterate them in PHP
foreach ($_POST["checkbox"] as $value){
// ..Delete FROM ... where userID = $value
}

Store textbox values in php array

So basically I have the user enter a number on my first screen.
Here is test1.php which generates the number of text boxes that the user had previously entered. That number is $input
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='Names'>";
}
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
Then my test2.php should print all the values entered, but it only prints out the last value entered from test1.php. For example is $input is 4, only the text entered in the 4th text box prints, which is understandable as I don't know how to print all values.
$names=$_POST['Names'];
foreach($number as $num){
echo $num;
}
Is the problem with the name I gave to the textboxes, or something else?
Any help is much appreciated.
Just create a name grouping attribute, so that you'll get an array of inputs instead of just one:
<input type='text' name='Names[]'>
// ^ this is important
Sidenote:
I don't know if this is a typo, but this should be $names instead of $number:
$names = $_POST['Names'];
foreach($names as $num){
echo $num . '<br/>';
}
Sample Demo
Your problem is that you give the same name to all of your input (in your test1.php) so when you try to restore them on your test2.php, your $_POST['Names'] just takes the last input with this name.
A solution is to give a different name to all of your input
In yout first file use it :
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='".$i."'>";
}
echo "<input type='hidden' name='input' value='".$input."'>";
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
And in your 2nd file :
for($i=1; $i<=$_POST['input']; $i++){
echo $_POST['$i'];
}
<form method="post" name="myform">
<input type="text" name="array[]" Value="101"/>
<input type="text" name="array[]" Value="102"/>
<input type="text" name="array[]" Value="103"/>
<input type="text" name="array[]" Value="104"/>
<input type="submit" name="submit" Value="submit"/>
</form>
if(isset($_POST['submit'])){
foreach($_POST['array'] as $myarray) {
echo $myarray.'<br>';
}
OUTPUT
101
102
103
104

Categories