i have this code.it is a dropdown list and a submit button.
I have a query "SELECT * DISTINCT column_name FROM table_name" that has result 15 values
Now,i want to take those values of the query and dynamicly enter them on the option=..... field.Also the "NAME" section must be the same as values.
example: IF value1="abcd"
<option value="abcd" >abcd</option>
<!DOCTYPE html>
<html>
<body>
<form method="post" target=".....php">
<select name="exa" >
<option value="value1" >NAME 1</option>
<option value="value2" >NAME 2</option>
<option value="value3" >NAME 3</option>
<option value="value4" >NAME 4</option>
<option value="value5" >NAME 5</option>
<option value="value6" >NAME 6</option>
<option value="value7" >NAME 7</option>
<option value="value8" >NAME 8</option>
<option value="value9" > NAME 9</option>
<option value="value10" >NAME 10</option>
<option value="value11" >NAME 11</option>
<option value="value12" >NAME 12</option>
<option value="value13" >NAME 13</option>
<option value="value14" >NAME 14</option>
<option value="value15" >NAME 15</option>
</select>
<form action=$value>
<input type="submit" value="GO!" />
</form>
</body>
</html>
i've made this but didnt work
<!DOCTYPE html>
<html>
<body>
<form method="post" >
<select name="exa" >
<?php
include_once "LOGIN TO DB SCRIPT";
$query_ak='SELECT DISTINCT (column_name) FROM table_name';
$result = mysql_query ($query_ak) or die (mysql_error);
while ($row = mysql_fetch_assoc($result)) {
}
?>
<option value = $row['ak_ex']> "$row['ak_ex']"</option>
<input type="submit" value="GO!" name="go"/>
</select>
</form>
</body>
</html>
You have made a lot of errors, first you've put the OPTION object outside the WHILE loop, then you put the submit button inside the SELECT object, in the end you write distinct as a function, the correct syntax is below.
I suggest you to use mysqli to avoid security problem and because mysql is going to be DEPRECATED. Use also include and not include_once because it requires extra work from PHP to render the file(Little difference but everything is welcome).
I've modified the code to use it, you find all the information to modify your script for working with mysqli at http://php.net/manual/it/book.mysqli.php
<html>
<body>
<form method="post" >
<?php
// LOGIN TO DATABASE SCRIPT WRITTEN FOR MYSQLI
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// END OF LOGIN TO DB SCRIPT
include DATABASE CONFIGURATION;
$query_ak='SELECT DISTINCT column_name FROM table_name';
$result = $mysqli->query($query_ak);
?>
<select name="exa" >
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
}
?>
</select>
<input type="submit" value="GO!" name="go"/>
</form>
</body>
</html>
You should placed <option>...</option> inside loop. Place submit button outside </select> tag. You also have syntax error. mysql_error should be mysql_error().
<!DOCTYPE html>
<html>
<body>
<form method="post" >
<?php
include_once "LOGIN TO DB SCRIPT";
$query_ak='SELECT DISTINCT (column_name) FROM table_name';
$result = mysql_query ($query_ak) or die (mysql_error());
?>
<select name="exa" >
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
}
?>
</select>
<input type="submit" value="GO!" name="go"/>
</form>
</body>
</html>
Related
I'm trying to create a drop down menu and run specific SQL scripts depending to each option value.
Part of the php code is this (I've made labels more easy for you to understand what i want):
<div class="form-group RunningSQLScripts">
<label for="inputStatus">Running My Scripts</label>
<select id="inputStatus" name="RunningSQLScripts" class="form-control">
<option selected disabled>--</option>
<option value="RunScript1">Delete Whole Table</option>
<option value="RunScript2">Run Script 3</option>
</select>
</div>
Lets say the RunScript1 is supposed to run the SQL command
truncate table table_name1. How can i do that from inside php menu ?
The easiest way is to use onclick= and then the name_of_function ();
for example:
<div class="form-group RunningSQLScripts">
<label for="inputStatus">Running My Scripts</label>
<select id="inputStatus" name="RunningSQLScripts" class="form-control">
<option selected disabled>--</option>
<option onclick="RunScript1()">Delete Whole Table</option>
<option onclick="RunScript2()">Run Script 3</option>
</select>
</div>
And then according to the function's name.
Create the appropriate functions to run on click.
You can submit the form to the server and execute the request you need.
<?php
$db = new mysqli('localhost', 'db_user', '1234', 'db_name');
if($db->connect_errno)die('Error: '.$db->connect_error);
?>
<form name="runSQL" method="get">
<div class="form-group RunningSQLScripts">
<label for="inputStatus">Running My Scripts</label>
<select id="inputStatus" name="RunningSQLScripts" class="form-control">
<option selected disabled>--</option>
<option value="RunScript1">Delete Whole Table</option>
<option value="RunScript2">Run Script 3</option>
</select>
<input type="submit" value="Run">
</div>
</form>
<?php
$runSQL = $_GET['RunningSQLScripts'];
switch($runSQL){
case 'RunScript1':
$db->query("truncate table `name`");
echo '* Successfully deleted *';
break;
case 'RunScript2':
$db->query("SQL");
echo '* Successfully run *';
break;
}
?>
Easiest way to do it: fully working,
Just add
onchange='this.form.submit();'
<form id="companyForm" name="companyForm" class="form-horizontal" type=get>
<div class="form-group RunningSQLScripts">
<label for="inputStatus">Running My Scripts</label>
<select id="inputStatus" name="RunningSQLScripts" onchange='this.form.submit();' class="form-control">
<option selected disabled>--</option>
<option value="RunScript1">Delete Whole Table</option>
<option value="RunScript2">Run Script 3</option>
</select>
</div>
</form>
<?php
$RunningSQLScripts = $_GET['RunningSQLScripts'];
$con = mysqli_connect('host','username','password','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
if ($RunningSQLScripts == "RunScript1")
{
$sql="truncate TABLE `table_name1`;"; // truncate table table_name1
$result = mysqli_query($con,$sql);
}
mysqli_close($con);
?>
I'm trying to get that from a dropdown list from my html.
<FORM ID="" ACTION="save.php">
<TABLE BORDER=1 WIDTH=90%>
<TR>
<TH><p style="color:red">Time</p></TH>
</TR>
<TR>
<TD>
<SELECT NAME="Time" ID="Time" style="width:220px">
<OPTION>
<OPTION>7:30-8:30
<OPTION>7:30-9:00
<OPTION>7:30-10:30
</SELECT>
</TD>
What happens is when the user click the submit button, it automatically call save.php
<?php
$time = $_POST['Time'];
//create connection_aborted
$conn = mysqli_connect("localhost", "root", "", "scheduling");
//check connection
if($conn-> connect_error) {
die ("connection failed; ". $conn-> connect_error);
}
$sql = "INSERT INTO sched (Time)VALUES ('$time')";
$conn->close();
?>
This technique works on textbox, I don't know why can't get data from drop down list. Am I missing something? Please help to fix.
You need set the option value like this:
<select id="Time" required>
<option value="">Please Select</option>
<option value="7:30-8:30">7:30-8:30</option>
<option value="7:30-9:00">7:30-9:00</option>
<option value="7:30-10:30">7:30-10:30</option>
</select>
And if you have option with blank value, good to check in php first before insert like this
if (!empty($_POST['Time'])) $Time = $_POST['Time'];
Your option tags need a value attribute to send to the server. Try this:
<SELECT NAME="Time" ID="Time" style="width:220px">
<OPTION value=""></OPTION>
<OPTION value="7:30-8:30">7:30-8:30</OPTION>
<OPTION value="7:30-9:00">7:30-9:00</OPTION>
<OPTION value="7:30-10:30">7:30-10:30</OPTION>
</SELECT>
<select id="Time" name="Time">
<option disabled="disabled" selected="selected" value="">Please Select One</option>
<option value="7:30-8:30">7:30-8:30</option>
<option value="7:30-9:00">7:30-9:00</option>
<option value="7:30-10:30">7:30-10:30</option>
</select>
I am doing project on marks management system. In the project i have a task of updating the marks of student.Suppose if the updation is successful i should display a message that it is successful otherwise
unsuccessful should be displayed. I am doing my project using php and html. The code is as follows.
update marks
user_id: <input type="text" name="userid"><br>
Branch<select name="Branch">
<option value="cse">CSE</option>
<option value="eee">EEE</option>
<option value="ece">ECE</option>
</select>
Marks<br><input type="text" name="marks" size="40"></br>
<select name="Subject">
<optgroup label="CSE">
<optgroup label="sem 4">
<option value="dbms">DBMS</option>
</optgroup>
<optgroup label="EEE">
</optgroup>
<optgroup label="ECE">
</optgroup>
</select>
Semester<select name="Semester">
<option value="sem 1">SEM 1</option>
<option value="sem 2">SEM 2</option>
<option value="sem 3">SEM 3</option>
<option value="sem 4">SEM 4</option>
<option value="sem 5">SEM 5</option>
<option value="sem 6">SEM 6</option>
<option value="sem 7">SEM 7</option>
</select>
<input id="button" type="submit" name="submit" value="SUBMIT">
</form>
</body>
</html>
The following one is php code
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
function updatem()
{
session_start();
$marks=$_POST['marks'];
$branch=$_POST['Branch'];
$semester=$_POST['Semester'];
$subject=$_POST['Subject'];
$userid=$_POST['userid'];
if((!empty($_POST['userid']))
&&(!empty($_POST['marks']))
&&(!empty($_POST['Subject']))
&&(!empty($_POST['Semester']))
&&(!empty($_POST['Branch'])))
{
$query=mysql_query("UPDATE marks_list SET marks_obt=$marks
WHERE username_id=$userid
AND branch_id=(SELECT branch_id FROM branch WHERE branch_name='$branch') AND
semester_id=(SELECT semester_id FROM semester WHERE semester_name='$semester')
AND subject_code=(select subject_code FROM subcodes WHERE
branch_id=(SELECT branch_id FROM branch WHERE branch_name='$branch')
AND
semester_id=(SELECT semester_id FROM semester WHERE semester_name='$semester'))") or die("insertion unsuccessful".mysql_error());
header("Location: update_marks.html");
}
}
if(isset($_POST['submit']))
{
updatem();
}
?>
thanks in advance..
Add this code after your update query
if(mysql_affected_rows()>0)
{
$_SESSION['message']='This is your message';
}
Now in the file Where you want to display the value
Add this code
Important Note: Always start session at the top of the page.
session_start();
echo $_SESSION['message'];
I have been having problems returning results from my database using AJAX. I have tried to echo $diskspace and $price, both of which are returning undefined.
index.php
<form id="form" method="POST">
Diskspace:
<select id="Diskspace">
<option value="0 AND 1">$0 - 1GB</option>
<option value="1 AND 5">$1 - 5GB</option>
<option value="5 AND 10">$5 - 10GB</option>
</select></br></br>
Price:
<select id="Price">
<option value="0 AND 5">$0 - $5</option>
<option value="1 AND 5">$5 - $10</option>
<option value="10 AND 20">$10 - $20</option>
<option value="20 AND 40">$20 - $40</option>
<option value="40 and 500">>$40</option>
</select></br></br>
<input type="submit" id="submit" value="enter">
</form>
<div id="output"></div>
JS
$(document).ready(function(){
$("div#output").hide();
$("#submit").click(function(){
$.post('join.php', {
diskspace: $("#diskspace").val(),
price: $("#price").val()
},
function(data){
$("div#output").html(data);
$("div#output").show();
}
);
return false;
});
});
</script>
join.php
<?php
if (isset($_POST['diskspace'])){
mysql_connect("localhost","root","") or die('Could not connect');
mysql_select_db("webhost") or die ('Could not find DB');
$diskspace = $_POST['diskspace'];
$price =$_POST['price'];
$query = mysql_query("
SELECT * FROM data WHERE Diskspace BETWEEN $diskspace
AND Price BETWEEN $price
");
$count = mysql_num_rows($query);
if ($count == 0){
$output = "No such results, sorry.";
}else{
while($row = mysql_fetch_array($query)){
$diskspace = $row['Diskspace'];
$price = $row['Price'];
$host = $row['Provider'];
$output .= '<div>'.$host.' '.$diskspace.' '.$price.'</div>';
}
}
echo $output;
}
?>
Your HTML element has an id of Diskspace and you're looking for an element of diskspace.
Take the following for example:
<select id="Diskspace">
<option value="hello">hello</option>
</select>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
console.log( $('#diskspace').val() );
});
</script>
The result is undefined, and in new versions of jQuery, if the element cannot be found it will not pass the variable through ajax/post.
The same applies to your Price element.
Your select id is not same in your jquery. Change it like this:
<form id="form" method="POST">
Diskspace:
<select id="diskspace">
<option value="0 AND 1">$0 - 1GB</option>
<option value="1 AND 5">$1 - 5GB</option>
<option value="5 AND 10">$5 - 10GB</option>
</select></br></br>
Price:
<select id="price">
<option value="0 AND 5">$0 - $5</option>
<option value="1 AND 5">$5 - $10</option>
<option value="10 AND 20">$10 - $20</option>
<option value="20 AND 40">$20 - $40</option>
<option value="40 and 500">>$40</option>
</select></br></br>
<input type="submit" id="submit" value="enter">
</form>
<div id="output"></div>
replace:
diskspace: $("#diskspace").val(),
with
diskspace: $("#Diskspace").val(),
You didn't give your <select>s names:
<select id="Diskspace" name="Diskspace">
^^^^^^^^^^^^^^^^--- missing
No name, no form submission for that field. id does NOT count - it's used purely for DOM operations, and is not relevant for form submissions.
Beyond that, you're vulnerable to SQL injection attacks.
I want to write a code that should let me select from a drop down list and onClick of a button load the data from a mysql database.However I cannot access the value selected in the drop down menu.I have tried to access them by $_POST['var_name'] but still can't do it.
I'm new to PHP.
Following is my code:
<?php
function load(){
$department = $_POST['dept'];
$employee = $_POST['emp'];
//echo "$department";
//echo "$employee";
$con = mysqli_connect("localhost", "root", "pwd", "payroll");
$rs = $con->query("select * from dept where deptno='$department'");
$row = $rs->fetch_row();
$n = $row[0];
$rs->free();
$con->close();
}
?>
<html>
<head>
<title>Payroll</title>
</head>
<body>
<h1 align="center">IIT Department</h1>
<form method="post">
<table align="center">
<tr>
<td>
Dept Number:
<select name="dept">
<option value="10" selected="selected">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</td>
<td>
<input type="button" value="ShowDeptEmp" name="btn1">
</td>
<td>
Job:
<select name="job">
<option value="President" selected="selected">President</option>
<option value="Manager">Manager</option>
<option value="Clerk">Clerk</option>
<option value="Salesman">Salesman</option>
<option value="Analyst">Analyst</option>
</select>
</td>
<td>
<input type="button" value="ShowJobEmp" name="btn1">
</td>
</tr>
</table>
</form>
<?php if(isset($_POST['dept']) && $_POST['dept'] != "") load(); ?>
</body>
</html>
change button to submit
<input type="submit" value="ShowDeptEmp" name="btn1">
and
<input type="submit" value="ShowJobEmp" name="btn2">
Use a prepared statement instead of echoing $department into your SQL. If someone posted '; DROP TABLE dept; they could run arbitrary SQL commands (See SQL Injection).
OR you can use mysql_real_escape_string() when escaping $department if you don't want to use a Prepared Statement.