PHP generated html form not giving response - php

I was creating a page which will create a number of html forms in it's body using a php for loop and each form will submit the response to the page itself. But after creating it the webpage is looking fine, but none of the forms are working. Here is a picture of the page.
Webpage is looking completely fine.
And the code is here.
<?php
session_start();
date_default_timezone_set("Asia/Dhaka");
if(!isset($_SESSION["user"])||$_SESSION["user"]!="shihan04"){
echo "<script> location.href=\"https://sgtcmc.000webhostapp.com/contests/index.php\";</script>";
}
if($_SERVER["REQUEST_METHOD"] == "POST"){
$pp = $_POST["pp"];
$rep = test_input($_POST["rep"]);
$dn = test_input($_POST["dn"]);
$dir=getcwd()."/clarifications/";
$fa=fopen($dir.$dn,"r");
$unm=fgets($fa);$ppb=fgets($fa);$tm=fgets($fa);$sub=fgets($fa);$qs=fgets($fa);$repb=fgets ($fa);
fclose($fa);
$fa=fopen($dir.$dn,"w");
fwrite($fa,$unm.$pp."\n".$tm.$sub.$qs.$rep);
fclose($fa);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Clarification Response</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="styles.css">
<script>MathJax = {tex: {inlineMath: [['$', '$'], ['\\(', '\\)']]},svg: {fontCache: 'global'}};</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax#3/es5/tex-svg.js"></script>
</head>
<body>
<?php
$ad=getcwd()."/clarifications/";
$af=scandir($ad,1);
$ac=count($af)-2;
for($i=0;$i<$ac;$i++){
$fa=fopen($ad.$af[$i],"r");
$unm=fgets($fa);$pp=fgets($fa);$tm=fgets($fa);$sub=fgets($fa);$qs=fgets($fa);$rep=fgets($fa);
fclose($fa);
echo "<table class=\"table-sm table-bordered\">
<tr><th>Username</th><td>".$unm."</td></tr>
<tr><th>Time</th><td>".$tm."</td></tr>
<tr><th>Subject</th><td>".$sub."</td></tr>
<tr><th>Question</th><td>".$qs."</td></tr>
</table>";
echo "<div class=\"container-sm\">
<form method=\"post\" action=\"".htmlspecialchars($_SERVER["PHP_SELF"])."\">
<div class=\"form-group\">";
if($pp=="1\n"){
echo "<select class=\"form-control\" id=\"pp\" name=\"pp\">
<option value=\"1\">Public</option>
<option value=\"0\">Private</option>";
}
else{
echo "<select class=\"form-control\" id=\"pp\" name=\"pp\">
<option value=\"0\">Private</option>
<option value=\"1\">Public</option>";
}
echo "</select>
</div>";
echo "<div class=\"form-group\">
<input type=\"text\" class=\"form-control\" name=\"dn\" value=\"".$af[$i]."\" disabled>
</div>
<div class=\"form-group\">
<input type=\"text\" class=\"form-control\" placeholder=\"Reply\" name=\"rep\" value=\"".$rep."\">
</div>
<input type=\"submit\" name=\"submit\" value=\"Submit\" class=\"btn btn-outline-primary\">
</form></div><br>";
}
?>
</body>
I can't find the solution for this and I need to fix this in next 2-3 days. I'm a complete noob. So please help me with this.

Related

Getting radio input from selection in table

I'm trying to get my code to open a page that displays a picture of the model and displays information. the code uses a csv file that populates the table. I cant seem to figure out how to get my radio button to pass data based on selection. I'm trying to find the selected row and display the data on another page basically. any help would be appreciated. Thank you!
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>Choose One</h1>
<form action="index.php" method="post">
<label for="Type">Package Type</label>
<select name="Type">
<option value="">Choose...</option>
<option value="a">apple</option>
<option value="b">google</option>
<option value="c">oneplus</option>
<option value="d">Samsung</option>
</select>
<input type="Submit" >
</form>
<?php
echo "<html><body><table border = '1'>\n\n";
echo "<form action='stock.php' method='post' name='stock'>";
echo "<th>Item</th><th>Manufacturer</th><th>Model</th><th>Price</th>";
$myfile = fopen("phones.csv", "r");
$ind=0;
$ind++;
while (($dataarray = fgetcsv($myfile)) !== false)
{
echo "<tr><td>"."<input type = 'radio' name='manufacturer' id=manufacturer
value=$dataarray[$ind]>"."</td>";
foreach ($dataarray as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
$ind = 0;
}
fclose($myfile);
echo "\n</table>
<input type='submit'>
</form></body></html>";
$ind = 0;
?>
</body>
</html>
Start of stock.php
<!DOCTYPE html>
<html lang="en">
<head>
<form action="index.php" method="get" name="stock">
</form>
<php?
if(isset($_get[manufacturer]))
{
$value=$_get[manufacturer];
}
?>
</head>
<body>
</body>
</html>
This is a snip of the csv file:

How do I pass these variables to another php file

I'm currently making a shopping website and I need to be able to pass two variables to the next page, the code may be badly written because I'm new to this, but I'm trying to pass the number from the drop-down menu and the "row['pid']" to another page. As shown below I have attempted to use a form button but it can only transfer the number from the dropdown. There is database connected so if you try to load it up, it may not load anything. This issue is specifically focussing on the button which the form is linked to at the end. Thank you for your time.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://kit.fontawesome.com/9114d9acc8.js" crossorigin="anonymous">
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="includes/navbar/navbar.css">
<link rel="stylesheet" href="css/style.css">
<title>Document</title>
</head>
<body>
<?php include "includes/navbar/navbar.php"; ?>
<div id="arranging">
<?php
require 'includes/dbh.php';
$query = sprintf('SELECT * FROM produce');
$result = mysqli_query($conn, $query);
//$var_value = 'hello';
//$_SESSION['varname'] = $var_value;
while ($row = mysqli_fetch_assoc($result)) {
if ($row['Quantity'] == 0) {
}
else {
?>
<div class="itemTemplate">
<a id="title" onclick="ContentPage(<?php echo $row['pid']; ?>)"><?php echo $row['Name'] ?></a>
<a onclick="ContentPage(<?php echo $row['pid']; ?>)" id="myid"><img src="<?php echo $row['img'] ?>"
alt="<?php echo $row['Name'] ?>"></a>
<span>Price: £<?php echo $row['Price'] ?></span>
<form action="includes/quickBasket.php" method="POST">
<div class="flex-b">
<label for="">Quantity:</label>
<select name="quantity">
<?php
if ($row['Quantity'] > 8) {
for ($x = 1; $x <= 8; $x++) {
echo "<option value='$x'>$x</option>";
}
}
else {
for ($x = 1; $x <= $row['Quantity']; $x++) {
echo "<option value='$x'>$x</option>";
}
}
?>
</select>
<button id="button" type="submit" name="cart" class="fas fa-shopping-basket"></button>
</div>
</form>
</div>
<?php
}
}
?>
</div>
<script>
function ContentPage(elem) {
location.href = "Product.php" + "?id=" + elem;
};
</script>
</body>
</html>
If you want to transfer the PID when the form is submitted then you need a field for it within the form, e.g. a hidden field like this:
<input type="hidden" name="pid" value="<?php echo $row['pid']; ?>"/>
Then when you submit the form, it will be accessible as $_POST["pid"] (just like the value from the dropdown is accessible as $_POST["quantity"]).
This applies to any value - if you want it to be submitted with the form, then there needs to be a proper field for it within the form (or least associated with the form via the necessary attribute).

unable to list values in LOV from mysql table using php

unable to list the information in LOV list from mysql table . I need to list the categories(catname) in the dropdown box and also let me know how to select the value(catno) for further action.
<?php
require_once('dbconnect.php');
$selsql="SELECT catno,catname FROM category"; //catno-integer and catname-
varchar
$res=mysqli_query($con,$selsql);
//$r=mysqli_fetch_assoc($res);
if (mysqli_query($con,$selsql)) {
}
else
{
echo "No connection";
die(mysqli_error($con));
}
?>
<html>
<head>
<title>Drop down list </title>
<meta charset=UTF-8">
<meta name="viewport">
</head>
<body>
<select name="categories"
style="width:250px;"onchange="this.form.submit();">
<?php while($row=mysqli_fetch_assoc($res)):;?>
<option value="<?php echo $row[0];?>"<?php echo $row[1];?></option>
<?php endwhile;?>
<!---<option value="<?php echo $row[0];?>""<?php echo $row[1];?>"</option>->
<!----$options.='<option value="'.$row[0].'"selected>'.$row[1].'</option>'->
</select>
</body>
</html>
This should work.
<?php
require_once('dbconnect.php');
$selsql="SELECT catno,catname FROM category";
$res=mysqli_query($con,$selsql);
if ($res == '')
{
echo "No connection";
die(mysqli_error($con));
}
?>
<html>
<head>
<title>Drop down list </title>
<meta charset="UTF-8">
<meta name="viewport">
</head>
<body>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php while($row=mysqli_fetch_assoc($res)) {?>
<option value="<?php echo $row['catno'];?>"><?php echo $row['catname'];?></option>
<?php } ?>
</select>
</body>
</html>

php form that requests data based on user select

I have edited this based on help already received. I seem to be having issues with how the information is being called from the database. I keep getting errors of no results found so to speak. I think I got a pretty good idea what might be the cause, but I am not sure to rectify this.
Originally this search function was set up differently. I had an html page that had a dropdown list each option was assigned a value, this being text i.e.
<option Value="North East">North East</option>
However because I have separate tables in the database that control the population of the drop downs, the value is no longer being text but a number.
Any ideas on how to combat this?
This is currently what I have from head to toe.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Results</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
}
});
});
});
</script>
<style>
body {
color: #FFFFFF;
}
#theclub{
border:1px solid white;
padding: 10px;
}
label
{
font-weight:bold;
padding:10px;
}
</style>
<link rel="stylesheet" type="text/css" href="Style.css">
<script type='text/javascript' src='style2.js'></script>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<label>Country :</label> <select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
} ?>
</select>
<label>City :</label> <select name="city" class="city">
<option selected="selected">--Select City--</option>
</select>
<input type="submit" value="Search" name="submit">
</form>
<?php
if(isset($_POST['submit']) && $_POST['submit'] != ""){ } //--here is the condition that is true if search button is pressed
$selectedOption = $_POST["city"];
$result = mysqli_query($con,
sprintf("SELECT * FROM `SouthYorkshire` WHERE `EstProv` = '%s'",
preg_replace("/[^A-Za-z ]/", '', $selectedOption)));
echo "<div id=\"Results\">";
while($row = mysqli_fetch_array($result))
{
echo "<div id=\"theclub\">";
echo "<div class=\"ClubName\">";
echo $row['EstName'];
echo "</div><br>";
echo "<div class=\"Location\">";
echo $row['EstAddress2'];
echo "</div>";
echo "<br>";
echo "<div id=\"website\"><img src=\"photos/more-info.png\" width=\"75\" height=\"25\"/> <img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div></div>";
echo "<br>";
}
echo "</div>";
mysqli_close($con);
?>
<br>
</body>
I'm shooting in the dark a bit because I'm not sure what you mean by first and second code...
If you want only one part of the code to run you need some sort of condition to check if to run it or not
If you don't want the second part of the code run right away you want to check if the post variable city is set and not empty and only run the code if the value isn't empty:
if(isset($_POST['city']) && $_POST['city'] != ""){
// your code here
}
Your second code should be enclosed within a if condition in order to run it after the first code exicution like the following way-
But first you have to add name attribute to your search button in first code-
<input type="submit" value="Search" name="submit">
and now your second code should be--
if($_POST['submit']) //--here is the condition that is true if search button is pressed
{
$selectedOption = $_POST["city"];
$result = mysqli_query($con,
sprintf("SELECT * FROM `SouthYorkshire` WHERE `EstProv` = '%s'",
preg_replace("/[^A-Za-z ]/", '', $selectedOption)));
echo "<div id=\"Results\">";
while($row = mysqli_fetch_array($result))
{
echo "<div id=\"theclub\">";
echo "<div class=\"ClubName\">";
echo $row['EstName'];
echo "</div><br>";
echo "<div class=\"Location\">";
echo $row['EstAddress2'];
echo "</div>";
echo "<br>";
echo "<div id=\"website\"><img src=\"photos/more-info.png\" width=\"75\" height=\"25\"/> <img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div></div>";
echo "<br>";
}
echo "</div>";
mysqli_close($con);
?>
}

Form not recognizing post variables

I have a simple html form and some php that input the POST variables into a mysql database. However, I noticed that the form would not input the data when
if (isset($_POST['submit'])){
insert stuff in here
}
was included. I then removed the if statement above and ran the code. All of the variables were imputed except the ones from the form using POST (ex $_POST['var1']). It seems like the POST variables are not being recognized and I dont know what's wrong.
ALL CODE:
<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
<?php include '../nav.php';
?>
<?php
if (isset($_POST['submit'])){
include '../connect.php';
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
$put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','subject','0')");
$result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'");
while ($row = mysql_fetch_assoc($result)){
$q=$row['id'];
}
}
?>
<form method='POST' action='question.php?q=<?php echo $q ?>'>
<p>Question:</p>
<p><input type='text' name='question' id='question' maxlength='200'></p>
<p>Add some detail (optional):</p>
<p><textarea id='detail' name='detail' ></textarea></p>
<p>Tags:</p>
<p><input type='submit' value='submit' name='submit'></p>
</form>
</div>
<?php include '../footer.php'; ?>
</body>
</html>
TESTPAGE:
<?php
include 'connect.php';
if (isset($_POST['submit'])){
$hhh=mysql_real_escape_string($_POST['hhh']);
$put=mysql_query("INSERT INTO questions VALUES ('','$hhh','','','','','','')");
}
?>
<form action='test.php' method='post'>
<input type='text' name='hhh'>
<input type='submit' name='submit' value='submit'>
</form>
You have two PHP pages - ask.php and question.php. I (guess)think the ask.php is used to store questions and other details and you want to open a question.php with question id.
ask.php
<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
<?php
include '../nav.php';
/*--- If submit button is pressed ---- */
if (isset($_POST['submit']))
{
include '../connect.php';
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
/* SELECT column names you want to use with INSERT statement */
$put=mysql_query("INSERT INTO questions
(`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` )
VALUES
('$question','$detail','$date','$time','$user','subject','0')");
//for debug purpose
if($put)
{
echo "Record added";
}
else
{
echo "Can't add record " . mysql_error();
}
}
/*----- End submit block -----------*/
/*----List the questions and select it----------*/
$time=time();
$user=$_SESSION['id'];
//I think this wont work. Try to remove time comparison from the SELECT statement.
$result=mysql_query("SELECT * FROM questions WHERE `user`='$user' AND `time`='$time'");
//$result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");
if($result)
{
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>$row[question]</td>";
echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No questions!!!";
}
?>
<form method='POST' action="ask.php">
<p>Question:</p>
<p><input type='text' name='question' id='question' maxlength='200'></p>
<p>Add some detail (optional):</p>
<p><textarea id='detail' name='detail' ></textarea></p>
<p>Tags:</p>
<p><input type='submit' value='submit' name='submit'></p>
</form>
question.php should be:
<?php
$qid=$_GET["qid"];
echo "$qid is selected...";
?>
You may split code into two PHP pages - one for save records and another to list and select rows.
addquestion.php
<?php
session_start();
if(isset($_POST['submit']))
{
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("your_db_name") or die(mysql_error());
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
$put=mysql_query("INSERT INTO questions
(`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` )
VALUES
('$question','$detail','$date','$time','$user','subject','0')");
//for debug purpose
if($put)
{
echo "Record added";
}
else
{
echo "Can't add record " . mysql_error();
}
}
?>
<form method='post' action="addquestion.php">
<p>Question:</p>
<p><input type='text' name='question' id='question' maxlength='200'></p>
<p>Add some detail (optional):</p>
<p><textarea id='detail' name='detail' ></textarea></p>
<p>Tags:</p>
<p><input type='submit' value='submit' name='submit'></p>
</form>
questionlist.php
<?php
session_start();
$user=$_SESSION['id'];
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("your_db_name") or die(mysql_error());
$result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");
if($result)
{
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>$row[question]</td>";
echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No questions!!!";
}
?>

Categories