multiple hidden fields - php

This is my first post, so I hope I'm doing it correctly.
This code displays several rows, and for each one there is a button that I would like
to redirect to the next form to edit the current line ID register.
I'm using variable hidden input names that are row-specific:
<?php
while($row = mysql_fetch_array($result))
{
$ID_variable[$count] = "ID".$row['ID'];
echo "<tr>";
echo "<td><input type=\"submit\" name=\"edit\" value=".$row['ID']."></td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td><input type=\"hidden\" name=".$ID_variable[$count]." value=".$row['ID']."></td>";
echo "</tr>";
$count++;
}
?>
So, I would like to pass the hidden name value for a given row to the next form I'm working with.
There must be a really simple solution, but I'm really stucked. Thanks for your time.

You should use javascript/jQuery to do this. This can also be done in php way but then you need to use forms.
<?php
while($row = mysql_fetch_array($result))
{
$ID_variable[$count] = "ID".$row['ID'];
echo "<tr>";
echo "<td><input onClick=\"nextForm(".$ID_variable[$count].",".$row['ID'].")\" type=\"submit\" name=\"edit\" value=".$row['ID']."></td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td><input type=\"hidden\" name=".$ID_variable[$count]." value=".$row['ID']."> </td>";
echo "</tr>";
$count++;
}
?>
<script language="javascript">
function nextForm(name,value) {
document.location = document.location + "?name="+name+"&value="+value;
}
</script>

I have the following code that I use to show data from DB and edit/delete:
<table>
<?php foreach ($row as $key => $value) { ?>
<tr>
<td><?php echo $value['id']; ?></td>
<td><?php echo $value['title']; ?></td>
<td><?php echo substr($value['text'], 0, 150); ?></td>
<td>
<img alt="" src="../libs/imgs/icons/edit.png" />
<img alt="" src="../libs/imgs/icons/edit.png" />
</td>
</tr>
<?php } ?>
</table>
I hope to be helpfull.

Related

How to random the choices in php

I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.
You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";

I want to know how to get the selected data on another webpage using PHP

I have use loop to show limited data and each loop i have button to select it.
I have a syntax error in my view.php i dont know what to put in that query
How do i select a row in index.php to view more details in view.php. I tried giving it a name in the echo $rows; loop but i got syntax error too.
This is my code in index.php
<?php
$sqlQuery = mysql_query("SELECT fname FROM info");
while ($rows = mysql_fetch_array($sqlQuery)){
echo $rows['fname'];
echo "<input type='submit' value='Show more info' name='submit' class='btn btn-info'>";
}
?>
-----------------------end code for index------------------------------
this is my code in view
<?php
$sqlQuery = mysql_query("SELECT * FROM info WHERE fname=$_POST['fname']");
echo "<table border='1' width='50%'>";
echo "<tr>";
echo "<td>First Name</td>";
echo "<td>last Name</td>";
echo "<td>Age Name</td>";
echo "</tr>";
while ($rows = mysql_fetch_array($sqlQuery)){
echo "<tr>";
echo "<td>";
echo $rows['fname'];
echo "</td>";
echo "<td>";
echo $rows['lname'];
echo "</td>";
echo "<td>";
echo $rows['age'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
-------------------------------end code for view--------------------
<form method="POST" action="view.php">
<?php
$sqlQuery = mysql_query("SELECT fname FROM info");
while ($rows = mysql_fetch_array($sqlQuery)){
echo "<input type='text' value='".$rows['fname']."' name='fname'>";
echo "<input type='submit' value='Show more info' name='submit' class='btn btn-info'>";
}
?>
</form>
Try this in view.php
echo $_POST['fname'];
$query = "SELECT * FROM info WHERE fname LIKE ".$_POST['fname']."";
echo $query;
check what output are you getting

Can't retrieve values from post variables that have a space in their name

I have a simple form which is designed to allow a user to enter numeric values and then sent them to a page which calculates the result.
The problem I'm having is that post variables that includes a space in their name don't seem to be recognised.
The form is created like so:
<?php
$minerals = mysqli_query($con, "SELECT * FROM items");
?>
<table border='1'>
<tr>
<td>Product: </td>
<td>Quanitity: </td>
<td>Buyback Price: </td>
</tr>
Minerals
</br>
</br>
<?php
while($row = mysqli_fetch_array($minerals)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . "Amount: <input type='text' value='0' name='" . $row['name'] . "'>" . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
if($row['name'] == "Morphite") {
break;
}
}
echo "</table>";
?>
And I'm trying to retrieve the post data like so:
echo '<table border="1">';
echo '<tr><td>Product</td><td>Value</td></tr>';
while($item = mysqli_fetch_array($items)) {
echo '<tr>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $_POST[$item['name']];
echo '</td>';
echo '</tr>';
}
echo '<tr><td>Total</td><td></td></tr>';
echo '</table>';
But any 'name' in the database that contains a space causes nothing to be displayed on the other end.
Since you can never be sure what is written in the database (invalid characters for html etc) you should encode this value before using it as inputs name-attribute. So try maybe htmlspecialchars or strip off the spaces and non-printable and invalid characters. Also if you have a primary key (integer) in your database you could use this to identify the record instead of the name.
Try replacing
echo $_POST[$item['name']];
with
echo $_POST["{$item['name']}"];

trying to retrieve name from current data selected

t
I have data shown about a band that matches a id selected, im then using the band_id and getting it to use it else where.
I would like to do the same with Name field but I am having a few problems, its just getting the word array in to the var $name.
code
<?php
require 'core/init.php';
$Band_id = $_GET['id'];
$name = ['Name']; // trying to get the band name
$result = mysql_query("SELECT * FROM bands WHERE Band_id = $Band_id");
echo "<table border = '1'>
<tr>
<th>Band Name</th>
<th>Venue</th>
<th>Category</th>
<th>Stock</th>
<th>Buy Ticket</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr><form name=\"myform\" action=\" order.php\" method=\"post\">";
echo " <input name=\"band\" type=\"hidden\" value=\"". $Band_id."\" >";
echo " <input name=\"bandn\" type=\"hidden\" value=\"". $name."\" >";
echo "<td>" .$row['Name']. "</td>";
echo "<td>" .$row['Venue']. "</td>";
echo "<td>" .$row['Category']. "</td>";
echo "<td>" .$row['Stock']. "</td>";
echo "<td><button>Buy Ticket</button></td>";
echo "</tr> </form>";
}
echo "</table>";
?>
$name = ['Name'] wont work above the query, and generally wont work at all as its invalid syntax for what you're trying to do. Where you have used $name use $row['Name']

PHP postback on submit

I have a question on php postback
My code is :
<?php
if(isset($_POST["Delete"]))
{
echo "DELETE";
}
if(isset($_POST["Modifier"]))
{
echo "Modifier";
}
if(!empty($_SESSION["Status"]))
{
if($_SESSION["Status"] == "u")
{
header("Location: Index.php?Action=Acceuil");
}
if($_SESSION["Status"] == "a")
{
$Connection = mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("tpw34") or die("Nope.");
$query = "Select * From Products";
$result = mysql_query($query);
While($ligne = mysql_fetch_assoc($result))
{
//Index.php?Action=AdminDeleteProduct&Delete=".$ligne["ProductID"]."
echo "<form method='POST' Action='#'>";
echo "<table border='1'>";
echo "<tr>";
echo "<td colspan='2'><center><img width='250' height='250' src='".$ligne["Image"]."'/></center></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Nom du produit :</th>";
echo "<td>".$ligne["ProductName"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Prix :</th>";
echo "<td>".$ligne["Prix"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Description :</th>";
echo "<td>".$ligne["Description"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'h><input type='Submit' value='Delete' name='Delete'/><input type='Submit' value='Modifier' name='Modifier'/></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "</form>";
}
}
}
?>
My Question is : I want to get the ProductID of the item ( in the table ) to be in the $_POST["Delete"] and $_POST["Modifier"] but i dont wanna change the text on the button. I want to keep DELETE and MODIFIER. I have read many things on the web but i dont find the correct answer.
Include a hidden form value for ProductID. Then you can retrieve the value in $_POST['ProductID']
echo "<input type=hidden name='ProductID' value='" . $ligne["ProductID"] . "'>";
You could work with sessions, where you can temporarily save your information.
Sessions
Or like Tim Dearborn suggested, use a hidden input to send it with the next form submit.

Categories