I will like to get multiple request after clicking submit. Meaning that after i clicked submit and get the result, i will like to inquire results again and click submit again. I don't want to lead myself back to nothingness.
<?php
error_reporting(0);
if (!$_POST['submit'])
{
<form action="http://localhost/test.php" method="post">
Type of Leave:
<select name="leave">
<option value=""selected="selected"></option>
<option VALUE="Medical Leave"> Medical Leave</option>
<option VALUE="Unpaid Leave"> Unpaid Leave</option>
</select>
<input type="submit" name="submit" value="Check!" />
</form>
<?php
}
else
{
$conn=odbc_connect("employee","","") or die (odbc_errormsg());
if (!$conn)
{
exit
("Connection Failed: " . $conn);
}
else
{
$choice = $_POST['leave'];
$sql="SELECT * FROM balance WHERE ID=$username";
$rs=odbc_exec($conn,$sql);
?>
<?php
while (odbc_fetch_row($rs))
{
$choice=odbc_result($rs,"$choice");
echo "<tr><td>$choice</td>";
}
odbc_close($conn);
echo "</table>";
}
}
Currently the code itself queries out the expected output that I want, but the only downside is it stays there as an output. What my aim is to have the submit and the select choice option stays there so that I can continue to output the result I want, thanks.
The best user experience would be to use AJAX to dynamically change the results table depending on what is chosen in the dropdown. For a simpler solution, you could try moving the form up and out of the if/else loop. Put the form first, then after the form prints, check for if ($_POST['submit']).
In this example, I also added a little function to add selected="selected" to whichever option was chosen for this submit. It's very rough, but you get the idea.
<?php
function matchPost($name,$val) {
if($val == $name)
echo 'selected="selected"';
}
?>
<form action="http://localhost/test.php" method="post">
Type of Leave:
<select name="leave">
<option value=""></option>
<option <?php matchPost('Medical_Leave',$_POST['leave']); ?> VALUE="Medical_Leave"> Medical Leave</option>
<option <?php matchPost('Unpaid_Leave',$_POST['leave']); ?> VALUE="Unpaid_Leave"> Unpaid Leave</option>
</select>
<input type="submit" name="submit" value="Check!" />
</form>
<?php
if ($_POST['submit'])
{
$conn=odbc_connect("employee","","") or die (odbc_errormsg());
if (!$conn)
{
exit
("Connection Failed: " . $conn);
}
else
{
$choice = $_POST['leave'];
$sql="SELECT * FROM balance WHERE ID=$username";
$rs=odbc_exec($conn,$sql);
?>
<?php
while (odbc_fetch_row($rs))
{
$choice=odbc_result($rs,"$choice");
echo "<tr><td>$choice</td>";
}
odbc_close($conn);
echo "</table>";
}
}
Related
I'm trying to create an html form to provide information about the salaries table in my database, the user should be able to pick year between 1986-1996 and choose if she wants to see the total salary of that year or the average salary of that year.
I have no idea how I link up these scripts and I can't find much online.
html file:
<html>
<body>
<fieldset>
<form id="frmName" method=post action="Oppgave4.php" onsubmit="">
<h1>Oppgave 4</h1>
Choose year:
<select id="frmName" onChange="">
<option selected disabled hidden>----</option>
<option name="1986">1986</option>
<option name="1987">1987</option>
<option name="1988">1988</option>
<option name="1989">1989</option>
<option name="1990">1990</option>
<option name="1991">1991</option>
<option name="1992">1992</option>
<option name="1993">1993</option>
<option name="1994">1994</option>
<option name="1995">1995</option>
<option name="1996">1996</option>
</select>
Total or average salary:
<select id="frmName" onChange="">
<option selected disabled hidden>----</option>
<option name="Total">Total salary</option>
<option name="Average">Average salary</option>
</select>
<input type="submit" value="Submit" id="submit">
</p>
</form>
</fieldset>
</body>
</html>
php file:
<?php
$year = ($_POST['1986'], $_POST['1987'], $_POST['1988'], $_POST['1989'], $_POST['1990'],
$_POST['1991'], $_POST['1992'], $_POST['1993'], $_POST['1994'], $_POST['1995'],
$_POST['1996'], $_POST['Total']);
$average = $_POST['Average'];
$conn = mysqli_connect("localhost", "root", "", "employees");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlavg = "SELECT AVG(salaries.salary) AS average FROM salaries
WHERE from_date = '$year'";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["average"] ."</td></tr>";
}
echo "</table>";
$sqlsum = "SELECT SUM(salaries.salary) AS total FROM salaries
WHERE from_date = '$year'";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["total"] ."</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
What you're trying to do is literally PHP/MySQL 101 and there is a lot online on how to do this. Having said that you are making some mistakes in your code. First, you should name the <select>
<select name="year">
Then you should give each option a value:
<option value="1994">1994</option>
...// do each one like this
This way, when the form is submitted to the PHP you can find it in the POST array:
$year = $_POST['year'];
That is just a start. You have a second drop-down that also needs a name and each option should have a value attribute.
<select name="calculation_type">
<option>----</option>
<option value="Total">Total salary</option>
<option value="Average">Average salary</option>
</select>
Which will be found like this in the POST array:
$average = $_POST['calculation_type'];
Your form needs a name and does not need the onsubmit The action should be the name of the PHP script which will perform the calculations:
<form name="form_name" method=post action="Oppgave4.php">
Warning
Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe!
Suggestion
You should go work through some basic PHP tutorials like those offered by https://www.learn-php.org/ (a free, interactive website) or other services
I begin to learn PHP because VBA is not enough.
And today I ask you to help.
I have Select box, which takes data from MySQL.
I have Input to which I would like to enter the data from the second column, the same table with that take the data to a select box and do not know how I do it.
If you want to add value from variable to input field you can do next
//$var you get from query from this your mysql table
<input type="text" name="somename" value="<?php echo $var['second_column']; ?>" id="someid">
are you asking for how to put data into a selectbox from database ?? If so, you can use this..
<select name="country">
<option value="">Select</option>
<?php
$countriesQuery=mysqli_query($conn,"SELECT id,name FROM countries");
while($countries=mysqli_fetch_assoc($countriesQuery))
{
echo "<option value='$countries[id]'>$countries[name]</option>";
}
?>
</select>
<select name="country">
<option value="">Select</option>
<?php
$countriesQuery=mysqli_query($conn,"SELECT id,name FROM countries");
while($countries=mysqli_fetch_assoc($countriesQuery))
{
echo "<option value='".$countries[id]."'>".$countries[name]." </option>";
}
?>
</select>
Hope this helps
<?php
$mysqli= new mysqli("localhost", "root", "", "db");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result=$mysqli->query("select name from table");
/* name is your second field*/
?>
<select name="">
<?php
while($res=$result->fetch_array())
{?>
<option value="<?php echo $res[0]; ?>"><?php echo $res[0]; ?></option>
<?php
}
?>
</select>
My first php code gets the values from the table "mytable" which are January, February and March respectively. It works well, I could see Month,January,February and March inside the drop down list. My problem is within the second php code, when I click the search button, nothing happens. What I expected it to do was print out the value from the drop down list but sadly, I get nothing.
<select name="monthchoice">
<option value="">Month</option>;
<?php
$request="SELECT date FROM mytable WHERE username='qwe'";
$result=mysqli_query($con, $request);
while($fetch = mysqli_fetch_assoc($result))
{
echo '<option value="'.$fetch['date'].'">'.$fetch['date'].'</option>';
}
?>
</select>
<input type="submit" value="Search" name="submit"/>
<?php
if(isset($_POST["submit"]))
{
if(!empty($_POST['monthchoice']))
{
$monthchoice1=$_POST['monthchoice'];
echo "<br>";
echo $monthchoice1;
}
else
{
echo "<br>";
echo 'Please choose a month!';
}
}
?>
Have you wrapped your select and input inside a form tag with method post?
<form action="" method="post">
<select name="monthchoice">
<option value="">Month</option>;
<?php
$request="SELECT date FROM mytable WHERE username='qwe'";
$result=mysqli_query($con, $request);
while($fetch = mysqli_fetch_assoc($result))
{
echo '<option value="'.$fetch['date'].'">'.$fetch['date'].'</option>';
}
?>
</select>
<input type="submit" value="Search" name="submit"/>
<?php
if(isset($_POST["submit"]))
{
if(!empty($_POST['monthchoice']))
{
$monthchoice1=$_POST['monthchoice'];
echo "<br>";
echo $monthchoice1;
}
else
{
echo "<br>";
echo 'Please choose a month!';
}
}
?>
</form>
I think you don't use the tag form.
I want my program to list my data from mysql table in a list in a dropdown menu on my page.
Here's my code:
<fieldset>
<legend> Selecteer uw Categorie </legend>
<label for ="Categorie"> Categorie </label>
<select name ="Categorie" id="Categorie">
<datalist id ="Categorie">
<Option Value="Router">Router</option>
<Option Value="Switch">Switch</option>
<Option Value="Toestel">Toestel</option>
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<Option Value= <?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<ul>";
echo "<li>" . $row['Categorieen1'] . "</li>";
echo "</ul>";
}
echo "</table>";
mysqli_close($con);
?>
</option>
</select>
</datalist>
</fieldset>
THis code works perfectly, It looks up the data i need and posts it in the dropdown list But it all gets posted in a single line..
I want it to be Listed underneath each other..
Please help me!
They are all on a single line because you put your result in a single <option> tag.
try this:
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "<option>Failed to connect to MySQL: " . mysqli_connect_error()."</option>";
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<option>".$row['Categorieen1'] . "</option>";
}
mysqli_close($con);
?>
</select>
</datalist>
</fieldset>
EDIT:
sorry for the mistake I forgot the second echo in the while loop XP!
I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?
Below is the entire code.
listCustomer
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result))
{
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
}
echo "</select>";
?>
<BR>
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.
Edit
Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:
<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR />
<?php
// BEGIN ADDED CONNECTION HACKY GARBAGE
$con=mysql_connect("localhost","root","root");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("sample",$con)
or die("Could not select examples");
// END ADDED CONNECTION HACKY GARBAGE
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
<BR />
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).
Secondly, this wont work (problem causer):
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
You should use
echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>";
or, as I always prefer single quotes to enclose echo or print output strings:
echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';
Third alternative (complex syntax: What does ${ } mean in PHP syntax?)
echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
assuming you get data from the database try this
echo "<option value={$r['Cnum']}>{$r['CName']}</option>";
try,
echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>";
instead of
echo "<option value=$r[Cnum]>$r[CName]</option>";