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.
Related
Is there any way to stop a button from submit button to stop looping in while loop?
it just looks terrible to have like 15 submit buttons insted of 1. whitout moving it from the from
$sql = "SELECT * FROM `bestalning` WHERE lev=0
ORDER BY tid";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<div class='continer bg-vit table-rsponsive-sm '>";
echo "<table class='table'>";
echo "<thead class='thead-dark'>";
echo "<tr><th>Lev</th>";
echo "<th> </th>";
echo "<th>Artikelnr</th>";
echo "<th>Antal</th>";
echo "<th>Singnatur</th>";
echo "<th>Önskad ankomst</th>";
echo "<th>Skickad</th>";
echo "</tr></thead><tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form method='POST' action='ID_change.php'>";
echo "<td><input style='margin-right:5px;' name='levJA' type='checkbox' value='1'></td>";
echo "<td><input style='margin-right:5px;' name='RowID' type='hidden' value='".$row['ID']."'></td>";
echo "<td>".$row['artikelnr']."</td>";
echo "<td>".$row['antal']."</td>";
echo "<td>".$row['ovrigt']."</td>";
echo "<td>".$row['Tid']."</td>";
echo "<td>".$row['date']."</td>";
echo "<input class='btn btn-dark m-1' type='submit' value='submit'>;
echo "</form>";
echo "</tr>";
}
echo "</tbody></table>";
echo "</div>";
mysqli_close($conn);
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 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
This question already has answers here:
Object Not Found error for simple php script
(4 answers)
Closed 9 years ago.
This code returns me a 404 Object Not Found error that I could not solve by modifying the code. And I don't know whether it's a code problem or a localhost problem. I need this to be running for my Final Year Project. Please help. Thanks a lot guys.
<?php
session_start();
require "dbc.php";
echo "<center>";
echo "<img src='web_header.jpg'/>";
echo "</center>";
if($_SESSION['admin_username'])
{
echo "Hello : ".$_SESSION['admin_username'];
echo "<p><a href ='admin_panel.php'>Admin Main</a> || <a href='manage_patients.php'>Manage Patients</a> || <a href ='manage_doctor.php'>Manage Doctors</a> || <a href ='mail_form.php'>Send Email</a> || <a href='logout.php'>Logout</a> ";
echo "<title>Manage Patients</title>";
echo "<h2>Manage Patients</h2>";
$query = mysql_query("SELECT * FROM users");
$numrows = mysql_num_rows($query);
if ($numrows != 0)
{
echo "<table width='1285' height='102' border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>IC Number</th><th>Address</th><th>Mobile Number</th><th>E-mail Address</th><th>Doctor ID</th></tr>";
while ($rows = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td width='57' height='33'>";
echo "<center>";
echo $rows['id'];
echo "</center>";
echo "</td>";
echo "<td width='173'>";
echo $rows['name'];
echo "</td>";
echo "<td width='113'>";
echo "<center>";
echo $rows['icnum'];
echo "</center>";
echo "</td>";
echo "<td width='622'>";
echo $rows['address'];
echo "</td>";
echo "<td width='110'>";
echo "<center>";
echo $rows['mobile'];
echo "</center>";
echo "</td>";
echo "<td width='170'>";
echo "<center>";
echo $rows['email'];
echo "</center>";
echo "</td>";
echo "<td width='90'>";
echo "<center>";
echo $rows['docID'];
echo "</center>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo"<br>";
echo"</br>";
echo "<form action = 'manage_patients.php method=POST'>";
echo "<p>Assign Doctor ID : ";
echo "<input type='text' name='assign_id' value='Insert Doctor ID Here'>";
echo " for Patient ID : ";
echo "<input type='text' name='id' value='Insert Patient ID Here'></p>";
echo "<input type= 'submit' name='submit' value='Assign Now'>";
echo "</form>";
if (isset($_POST['submit']))
{
require "dbc.php";
$name = mysql_real_escape_string($_POST['assign_id']);
$id = mysql_real_escape_string($_POST['id']);
$query = mysql_query("UPDATE users SET docID='$assign_id' WHERE id='$id'");
echo "Doctor ID Successfully Assigned for Patient ID : $name ";
echo "<p>Refresh page to view changes.</p>";
}
}
}
else
{
header("location:index.html");
}
?>
change this
<form action = 'manage_patients.php method=POST'>
to
<form action = 'manage_patients.php' method='POST'>
If you are getting a 404 Error, it means that the page could not be found. So it is most likely not a problem with your code (that would give you a 500 Server Error). Instead you most likely have used the wrong URL.
<form action = 'manage_patients.php method=POST'>
I find this line of yours a bit suspiscious
I am trying to get a page to redirect after I click the button and after the database updates but it doesnt seem to work.
I have a form that completes in itself, to cut down on the number of pages used (e.g no, thanks content has been edited" page) but after the content has been edited i wish to redirect back home.
form and database update:
<div id = "errormsg"> </div>
<?php
echo "<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"7\" style=\"font-family:Arial;font-size:11px\">";
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><form name=\"confhome\" method=\"post\" action=\"\" onsubmit=\"return valText()\"><textarea name=\"tag\" cols=\"20\" rows=\"3\" id=\"tag\">$row[tagbrief]</textarea></td></tr>";
echo "<tr><td><input type=\"submit\" value=\"Edit\"></form></td></tr>";
echo "<tr></tr>";
echo "</table><br/>";
$tagbrief = $_POST['tag'];
mysql_query("UPDATE quackedup SET tagbrief='$tagbrief' WHERE id='1'");
?>
JS validation for ref
<script type="text/javascript">
function valText(){
var text = document.getElementById('tag');
var div = document.getElementById('errormsg');
var lets = /^[0-9a-zA-Z\s\-\'(\)\&\,\:\.\!\?]+$/;
if((text.value == '') || (text.value == ' ')){
div.innerHTML="<b>Please enter your changes</b>";
text.focus();
return false;}
else if(text.value.match(lets)){
div.innerHTML="<b>Content updated</b>";
return true;}
else {
return false;}
}
Any help appreciated, thanks.
I'm not sure I fully follow but I would rejigger it so it was like this:
<?
if($_POST)
{
$tagbrief = mysql_real_escape_string($_POST['tag']);
mysql_query("UPDATE quackedup SET tagbrief='$tagbrief' WHERE id='1'");
header("Location: /path/to/script");
exit;
}
?>
<div id = "errormsg"> </div>
<?php
echo "<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"7\" style=\"font-family:Arial;font-size:11px\">";
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><form name=\"confhome\" method=\"post\" action=\"\" onsubmit=\"return valText()\"><textarea name=\"tag\" cols=\"20\" rows=\"3\" id=\"tag\">$row[tagbrief]</textarea></td></tr>";
echo "<tr><td><input type=\"submit\" value=\"Edit\"></form></td></tr>";
echo "<tr></tr>";
echo "</table><br/>";
?>
Now, it only updates the field if you are actually posting the form, and the form contents are escaped for the database (to prevent SQL injection). Just change the /path/to/script to be the path/url you want to go to.
I think you just need to write
header("Location:/");
I hope this will help you