So I have a database with 2 tables and a page with 2 select lists.
According to the value selected on the first select, I want to show the corresponding items on the second select.
There are no errors or anything, but the second select does never show anything, so I'm probably missing something.
Can someone help me?
Connection.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dberror1 = "Could not connect to the database";
$dberror2 = "Could not find the table";
$Conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$Select_db = mysqli_select_db($Conn, 'swimming') or die ($dberror2);
?>
Results.php
<?php
include ("Connections/connection.php");
$query_comp = mysqli_query($Conn, "Select * FROM comp");
?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>Swimming Results</title>
<link href="file:///D|/Websites/Swimming/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
<!-- Div for Title -->
<div id="title">
<p>Swimming Results</p>
</div>
<form name="results" >
<!-- Div to Choose competition -->
<div id="competition">
<p>Competition</p>
<select name="sel_comp">
<option value="">---Select one competition---</option>
<?php
while ($fetch = mysqli_fetch_assoc($query_comp)){
echo "<option value='{".$fetch['id']."}'>".$fetch['comp_name']."</option>";
}
?>
</select>
</div>
<!-- Div to Chose event -->
<div id="event">
<p>Event</p>
<select name="sel_even">
<option value="">---Select one event---</option>
<?php
$chosen=$_POST["sel_comp"] ;
$query_even = mysqli_query($Conn, "Select comp.id, events.cid, events.event_name FROM events, comp WHERE events.cid = $chosen");
while ($fetch = mysqli_fetch_assoc($query_even)){
echo "<option value='{".$fetch['event_id']."}'>".$fetch['event_name']."</option>";
}
?>
</select>
</div>
<!-- Div to Show results -->
<div id="results">
<p>Resultados</p>
</div>
</form>
</div>
</body>
</html>
Your PHP code is interpreted server-side, before displaying to the user's browser. So this code can't know anything about the choices the user will do.
If you want a "dynamic sub-select", you need to add some javascript code. That javascript code will run on the user's browser and will be able to react to the user's action on the first select box.
Related
I am working on this program that gets some inputs from the user and then uses that input to retrieve data from a few tables in a MySQL database. My issue is that it seems like I am not getting anything from the MySQL tables, and it would be great if you could help.
dbConn.php
<?php
$ser = "localhost";
$user = "root";
$pass = "pass";
$db = "dbvisual";
$conn = mysqli_connect($ser, $user, $pass, $db) or die("connection failed");
echo "connection success";
?>
form.php
<?php
if(isset($_Post['submit'])){ // Checking to see if the form has been submitted
$battery = (int) $_POST['battery']; // Battery Input element
$cycle = (int) $_POST['cycle']; // Cycle input element
$xVar = $_POST['X']; // X variable
$yVar = $_POST['Y']; // Y variable
// Trying to get the x variable based on what the user said
$mySqlQueryX = "SELECT cap
FROM cycle
WHERE cycleID = $cycle";
$feedbackX = mysqli_query($conn, $mySqlQueryX);
echo "<h1>$feedbackX</h1>";
}
?>
index.php
<!-- Connecting to the database -->
<?php
include 'dbConn.php';
?>
<!-- The Styling of the website -->
<style>
<?php include 'main.css'; ?>
</style>
<!-- The Skeleton of the website -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php include "form.php" ?>
<!-- The entire website -->
<div id="body">
<!-- The input half -->
<div id="inputs">
<!-- The input form -->
<form action="index.php" method="POST">
<div id="form">
<!-- Labels -->
<div id="label">
<label for="">Which Batteries?</label>
<label for="">What Cycles?</label>
<label for="">What's X?</label>
<label for="">What's Y?</label>
<label for="">Discharge?</label>
<label for="">Charge?</label>
</div>
<!-- User Info -->
<div id="info">
<input type="text" placeholder="Batteries" required
oninvalid="this.setCustomValidity('No Batteries Were Entered')"
oninput="setCustomValidity('')" name="battery">
<input type="text" placeholder="Cycles" required
oninvalid="this.setCustomValidity('No Cycles Were Entered')"
oninput="setCustomValidity('')" name="cycle">
<select name="X">
<option value="cap">Capacity</option>
<option value="speCap">Specific Capacity</option>
<option value="voltage">Voltage</option>
</select>
<select name="Y">
<option value="cap">Capacity</option>
<option value="speCap">Specific Capacity</option>
<option value="voltage">Voltage</option>
</select>
<input type="checkbox" name="discharge" checked>
<input type="checkbox" name="charge" checked>
</div>
</div>
<!-- Submit Button -->
<div>
<button type="submit" name="submit">Submit</button>
</div>
</form>
</div>
<!-- The graph half -->
<div id="graph">
<p>hello</p>
</div>
</div>
</body>
</html>
When I try to "echo" what I am supposed to get from the database, nothing shows up and I am guessing the issue could be related to mysqli_query() having problems with $conn.
Thank you all!
You need to change your form.php file this is what I propose
<?php
if(isset($_POST['submit'])){
$battery = (int) $_POST['battery']; // Battery Input element
$cycle = (int) $_POST['cycle']; // Cycle input element
$xVar = $_POST['X']; // X variable
$yVar = $_POST['Y'];
$con = mysqli_connect("localhost","root","passer","arduino");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$mySqlQueryX = "SELECT cap
FROM cycle
WHERE cycleID = '".$cycle."' ";
$result = mysqli_query($con, $mySqlQueryX);
// Fetch all
print_r(mysqli_fetch_all($result, MYSQLI_ASSOC));
// Free result set
mysqli_free_result($result);
mysqli_close($con);
}
Hi I'm trying to create a web-page that selects and displays the SQL row connected to the selected name from a drop-down menu. I've gotten everything to work except actually displaying the row when selected by the drop-down, here is some code I've cobbled together from various sources including Stack Overflow.
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
//if your connection succeded
$sql = "SELECT * FROM Charities" ;
$result = mysqli_query($conn,$sql);
?>
<form method="post" action="<?= $_SERVER['PHP_SELF']; ?>">
<select name="list">
<?php while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)): ?>
<option value="<?= $row['Name']; ?>"><?= $row['Name']; ?></option>
<?php endwhile; ?>
</select>
<input type="submit" value="See Points">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$("select[name=list]").on("change", function () {
var valueSelected = $(this).val();
$("#value-selected").html(valueSelected);
});
});
</script>
Currently I'm a little confused where to place the php prompt to display the information, I'm very new to PHP and SQL. Thank you for your help!
You can just add :
<p id="value-selected"></p>
You can add it between the form and the first script or before the form, or anywhere else in the html code.
I have been trying to get it so that I can assign a variable to the value that the user selects in my drop-down menu of countries. I have done this before with tables, but not drop down lists, so I'm not sure how. Ultimately, I am trying to have a new page return a table for the country selected once the user clicks the submit button, and know I need a varaiable assigned to what the user selects. Here is my code so far:
<html>
<head>
<title>test2</title>
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<?php include("header.php"); ?>
<article>
<!-- This describes the choice of country name selection -->
<h1>Please select your country:</h1>
<form action='processformD.php' method='GET'>
<select name='CountryName'>
<?php
$query = "SELECT * FROM alphabetizedCountryNames;";
$result=mysqli_query($con, $query) or die ("Couldn't execute query.");
// $row = mysqli_fetch_assoc($result);
?>
<?php
while($row = mysqli_fetch_array($result))
{
extract($row);
echo "<option value='$CountryName'>$CountryName\n";
}
?>
</select>
<div class="centerthatshit"><?php echo "<p><input type='submit' value='submit' /></p>\n"; ?></div>
</form>
</article>
<?php include("footer.php"); ?>
</body>
</html>
</body>
</html>
I'm trying to retrieve data from my database using and HTML form and php connection to my sql-workbench. I have a successful connection to the data base and my retrieval is posted at the top of my webpage. I'm not sure how to get it to run the sql query on the submit form function.
Any help would be appreciated, thanks.
<!DOCTYPE html>
<html>
<head data-gwd-animation-mode="quickMode">
<title>Test_webpage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="generator" content="Google Web Designer 1.1.3.1119">
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "006";
$dbname = "mydb";
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
//$con->close();
//$query = "SELECT * FROM US_Cars";
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT Manufacturer,VIN,Color,Model,Fuel_Type,State_of_Origin FROM mydb.US_Cars;";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Manufacturer: " . $row["Manufacturer"]. " VIN: " . $row["VIN"]. " Color:" . $row["Color"]. " Model:" . $row["Model"]. " Fuel Type:" . $row["Fuel_Type"]. " State Location:" . $row["State_of_Origin"]. "<br>";
}
} else {
echo "0 results";
}
?>
<div id="first">
<p>Enter Criteria Below to Search.</p>
</div>
<br>
<div id="second">
<p class="gwd-p-j2b6">Select a Manufacturer</p>
<!-- start of form -->
<form class="gwd-form-yuwo" method="post" action="<?search2.php ?>">
<select class="auto-style3 gwd-select-u9g0">
<option value="GM">GM</option>
<option value="Ford">Ford</option>
<option value="Chrysler">Chrysler</option>
<option value="Tesla">Tesla</option>
</select>appreaciated
<br>
<br>
<p class="gwd-p-cy97">Select a Dealership Location</p>
<select class="auto-style3 gwd-select-dkho">
<option value="">Virginia</option>
<option value="">Maryland</option>
<option value="">New Jesery</option>
<option value="">Kentucky</option>
</select>
<br>
<input type="submit" value="Submit" class="auto-style2">
</form>
</div>
<div id="third">
<img id="pic1" src="page2_1.jpg" style="width:500px;" height="236px" >
</div>
<div id="forth">
<img src="tesla.png" style="width:125px;height:250px">
</div>
<div id="fifth">
<footer>
<p>
U.S. Car data Entry
</p>
<p>
About Page
</p>
</footer>
</div>
</body>
</html>
<form class="gwd-form-yuwo" method="post" action="<?search2.php ?>">
is your issue.. pretty sure that is supposed to be
<form class="gwd-form-yuwo" method="post" action="search2.php">
which would send you to search2.php when the submit button was pressed... then in search2.php you would get your form values from $_POST and then run your query.
That being said I think that is not what you are trying to do and what you really want to do is update a list of things within the webpage without linking through. i.e. on button press update html at top of page to list query results using prams from form. If that is what you are trying to do you are way off and need to look up ajax... your submit button would not have a direct action but instead run a javascript fuction that would send an ajax request to search2.php that would run the query and then your java script would update innerhtml on response.
I made some PHP code to generate this page. I successfully get all the items from a column into a HTML dropdown list (it's a dynamic list). I want to write some code so that when user selects an item from the list and hit submit, it will take user to a new page contains corresponding information on it. I have no idea what kind of code would be included in. Please help. Thanks!
For instance, if user select 50A-1, it will populate a table has all the items located at 50A-1.
Two pieces of code I wrote, first is the page gives you the dropdown list and the submit button. The second is the result page, but it only shows the whole inventory so far, it doesn't have a way to connect to the dropdown list option.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Inventory</title>
</head>
<body>
<div>
<a>SQL Connection test</a>
<form action="connect.php" method="POST">
<div class="center">
<input type="submit" value="Connect to MySQL" />
</div>
</form>
</div>
<div>
<section>
<article>
<p>
<select name="dropdown">
<?php query() ?>
</select>
<?php close() ?>
</p>
</article>
</section>
<div>
<input type="submit" value="Submit" />
</div>
</div>
</body>
</html>
Second page
<?php
include_once 'db.inc.php';
// connect
function connect() {
// Connect to the MySQL server
mysql_connect(DB_HOST,DB_USER,DB_PASS) or die ('Could not connect to server!' . mysql_error());
mysql_select_db(DB_NAME);
}
// close
function close() {
mysql_close();
}
// query
function query() {
$myData = mysql_query("SELECT DISTINCT * FROM sheet0_100 GROUP BY location");
while($record = mysql_fetch_array($myData)) {
echo '<option value="' . $record['location'] . '">' . $record['location'] . '</option>';
}
}
?>
That's the purpose of HTML forms :)
You need to create a form to encapsulate that select:
<form action="process.php" method="get">
<select name="inventory_id">
<!-- Here all options -->
</select>
<button type="submit">See items</button>
</form>
Then in process.php you need to get the selected element and query the database, for example (I assume that you're using PDO):
<?php
$inventory_id = $_GET['inventory_id'] // The name attribute of the select
// Then you prepare the query
$query = "SELECT * FROM sheet0_100 WHERE id = :inventory_id";
// Execute the query and show the data...
use Sessions
example:
on your first page
session_start();
$_SESSION['your-dropdown-list-value'] = 'Root';
on your new page
//error_reporting(E_ALL);
session_start();
if(isset($_SESSION['your-dropdown-list-value'])) {
echo "Your dropdown selection " . $_SESSION['your-dropdown-list-value'];
}