i mange to populate the select by calling data on database
how will i call other data related from the value of the select using database.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
</head>
<body>
<?php
$conn = new mysqli('localhost', 'root', 'a', 'emp_table')
or die ('Cannot connect to db');
$result = $conn->query("select * from emp");
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>
</body>
</html>
Try this
As per #Nikhil suggested the link
https://www.w3schools.com/xml/ajax_intro.asp
You can achieve this using AJAX. Time to learn the AJAX now.
For connection
If you are working on the localhost the no need to require the password. It should be blank.
I just added the onchange function to the select tag. So whatever the user selects from the dropdown it will call the function getRole and pass the Id to the process page. Process page will return the value related to the ID and display in the span tag.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emp_table";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
</head>
<body>
<select name="emp_name" id="emp_name" onchange="getRole(this.value);" >
<option selected disabled>Select</option>
<?php
$result = $conn->query("select * from emp");
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$name = $row['name'];
?>
<option value="<?php echo $id;?>"><?php echo $name;?></option>
<?php }}?>
</select>
<span id="emp_details"></span><!--it will display the emp record from process page-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function getRole(val){
var id=$('#emp_name').val();
$.ajax({
url:'process.php',
type:'POST',
data:'id='+id,
success:function(data)
{
//alert(data);
$("#emp_details").html(data);
},
});
}
</script>
</body>
</html>
Process.php
<?php
if (isset($_POST['id'])) {
echo $country_id=$conn->real_escape_string(trim($_POST['id']));// here you will get the id of the employee
/*your more code here*/
}
?>
Related
This is a simplified version of a project I'm working on.
The main idea is that test.php sends 'acct-id' to submitData.php and it validates if the account ID already exists in the server. In case it already exists, I want to send an error message to test.php.
I have built this code but I can't get '$err_msg' to change it's value in test.php. This variable changes it's value within the 'while' loop but it does not change outside it.
How can I send $err_msg value to test.php?
Thanks in advance to all possible help.
test.php
<?php
include 'submitData.php';
?>
<!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>
<form action="submitData.php" method="POST">
<input type="text" name="acct-id">
<input type="submit" name="submit-btn">
<?=$err_msg?>
</form>
</body>
</html>
submitData.php
<?php
include '../tools/db_config.php';
$err_msg = "test";
if(isset($_POST["submit-btn"])){
$acct_id = $_POST["acct-id"];
$sql = "SELECT * FROM accounts WHERE acct_id='$acct_id'";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$err_msg = "This account already exists.";
}
header('Location: test.php');
}
}else {
$output="Error en la consulta: ".$conn->error;
}
}
?>
You can use Session for this.
<?php
include '../tools/db_config.php';
session_start();
$err_msg = "test";
if(isset($_POST["submit-btn"])){
$acct_id = $_POST["acct-id"];
$sql = "SELECT * FROM accounts WHERE acct_id='$acct_id'";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$err_msg = "This account already exists.";
}
$_SESSION['err_msg'] = $err_msg;
header('Location: test.php');
}
}else {
$output="Error en la consulta: ".$conn->error;
}
}
?
<?php
include 'submitData.php';
?>
<!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>
<form action="submitData.php" method="POST">
<input type="text" name="acct-id">
<input type="submit" name="submit-btn">
<?=$_SESSION['err_msg']?>
</form>
</body>
</html>
I have a bit of code where I try to make a switch program.
Meaning that I want to have one value when clicking on a button and then another if I click it again. But I just can't figure out how.
It should echo "Yes" and then "No", front and back.
Anybody here that knows how I can solve this problem? I have been trying for hours on end, practicing my PHP.
<?php
session_start();
var_dump($_SESSION);
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_SESSION['NO']) && $_SESSION['NO'] = true) {
echo "No";
$_SESSION['YES'] = true;
}
if(isset($_POST['submit'])) {
echo "Yes";
$_SESSION['NO'] = true;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method="post">
<input type="submit" name="submit" value="CHANGE">;
</form>
</body>
</html>
Use ONE session variable, like $_SESSION['STATE'] then the management of its state is simpler.
Also = is an assigment operator and == is for value comparison.
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['submit'])) {
// reverse the state
$_SESSION['STATE'] = ! $_SESSION['STATE'];
}
} else {
// page running for the first time so has not been submitted
// so initialise the session
// this will ensure you get a value output the first time the page is run
$_SESSION['STATE'] = false;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>Current State = <?php echo $_SESSION['STATE'] ? 'YES' : 'NO'; ?></p>
<form method="post">
<input type="submit" name="submit" value="CHANGE">;
</form>
</body>
</html>
Set an initial value for the session variable if it does not exist and use 1- $value ( where $value is either 1 or 0 ) to toggle the value.
<?php
session_start();
if( !isset( $_SESSION['state'] ) ) $_SESSION['state']=0;
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$_SESSION['state'] = 1 - $_SESSION['state'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php
if( isset( $_SESSION['state'] ) ){
echo $_SESSION['state'] ? 'Yes' : 'No';
}
?>
<form method="post">
<input type="submit" name="submit" value="CHANGE" />
</form>
</body>
</html>
I'm trying to use data sessions for checking a form, but, when I send user and password, and programme goes to check password, this return to main page.
I write 1234 on password field, but I can't understand why this don't work correctly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Check</title>
</head>
<body>
<?php
$usuario = trim(htmlspecialchars($_REQUEST['username'], ENT_QUOTES, "UTF-8"));
$clave = trim(htmlspecialchars($_REQUEST['password'], ENT_QUOTES, "UTF-8"));
setcookie("usuario", $usuario, time()+60*60*24*365);
session_start();
$_SESSION['nom_user'] = $usuario;
$_SESSION['pass_user'] = $clave;
header('Location: nacimiento.php');
?>
Volver
</body>
</html>
Second Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Fecha de Nacimiento</title>
</head>
<body>
<?php
session_start();
if (($_SESSION['pass_user'])=='1234'){
echo '<form action="check.php" method="POST">';
echo '<label for="fecha_nac">Fecha de Nacimiento</label><input type="date" name="fnacim" id="fnacim" />';
echo '<input type="submit" name="Enviar" />';
echo '</form>';
} else {`enter code here`
header('Location: index.php');
}
?>
Volver>
</body>
session_start() must be called before any output to the browser.
Please update your code on all php pages that use the session to be included first.
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Fecha de Nacimiento</title>
</head>
<body>
<?php
if (($_SESSION['pass_user'])=='1234'){
echo '<form action="check.php" method="POST">';
echo '<label for="fecha_nac">Fecha de Nacimiento</label><input type="date" name="fnacim" id="fnacim" />';
echo '<input type="submit" name="Enviar" />';
echo '</form>';
} else {`enter code here`
header('Location: index.php');
}
?>
Volver>
</body>
I have been trying to build a database for my father who owns a model circus store and I have run into a few issues this particular issue I cannot identify. When I run the search it returns empty results. I've checked and double checked my code with one of my websites that works. Where did I go wrong?
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "circus.db.10527209.hostedresource.com";
$username = "circus";
$dbname = "circus";
//These variable values need to be changed by you before deploying
$password = "********";
$usertable = "Customers";
$yourfield1 = "Customer_Name";
$yourfield2 = "Address";
$yourfield3 = "Phone_Number";
$yourfield4 = "Email_Address";
$yourfield5 = "Scale";
$yourfield6 = "Project_Type";
$yourfield7 = "Number";
$yourfield8 = "CMB_Member";
$search_id = $_POST["search_id"];
//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
//Fetching from your database table.
$query = "SELECT * FROM $usertable WHERE Customer_Name = $search_id" ;
//echo $query;
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$yourfield1"];
$address = $row["$yourfield2"];
$phone = $row["$yourfield3"];
$email = $row["$yourfield4"];
$scale = $row["$yourfield5"];
$type = $row["$yourfield6"];
$number = $row["$yourfield7"];
$CMB = $row["$yourfield8"];
}
}
?>
<head>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta name="viewport" content="width=device-width"/>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h2> User Data</h2>
Name:<?php echo $name; ?><br>
Customer Address:<?php echo $address; ?><br>
Phone Number:<?php echo $phone; ?><br>
Email Address:<?php echo $email; ?><br>
Scale: <?php echo $scale;?><br>
Project Type: <?php echo $type; ?><br>
Customer Number:<?php echo $number;?><br>
Circus Model Builder:<?php echo $CMB; ?><br>
</span>
</body>
<footer>
</footer>
</html>
Search Page:
<head>
<meta name="viewport" content="width=device-width">
<title>Search</title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="/style.css" media="screen" />
</head>
<body>
<div id="wrap">
<div id="top"></div>
<div id="content">
<div id="header">
</div>
<div id="content">
<p>Enter Customer Name Here<p>
<form action="viewuser.php" method="POST">
<fieldset>
<input type= text id="search_id" name= "search_id"
cols= "7" rows="1"></input>
<feildset>
<br/>
<fieldset class"auto">
<input type="submit" value="Find Customer"/>
<input type="reset" value="Restart"
</fieldset>
</form>
</div>
<div id="footer">
<center>Database information as of 6/13/13</center>
</a>
</div>
</body>
</html>
Is $search_id actually an integer? Besides the injection attack risk (you're not doing any checking/escaping of the submitted value) and the fact that you shouldn't be using the mysql_* functions, it looks like it's a string, which means it needs to be quoted in your query:
$query = "SELECT * FROM $usertable WHERE Customer_Name = '$search_id'";
I have been trying to get this sign up php to work. I'm close, I guess. But there's a problem. The result is always a button that redirects to the homepage(You'll know what I'm talkin about when you read the code.) If you could find the problem please tell me.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
<link href='icon.jpg' rel='icon' type='image/jpg'/>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
<LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
<Title>Sign up to JM Today</title>
</head>
<body>
<?php
$dbservertype='mysql';
$servername='localhost';
$dbusername='***';
$dbpassword='***';
$dbname='jmtdy';
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect("$servername","$dbuser","$dbpassword");
if(!$link){
die("Could not connect to MySQL");
}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
connecttodb($servername,$dbname,$dbusername,$dbpassword);
?>
<?php
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$password2=mysql_real_escape_string($_POST['password_confirmation']);
$todo=mysql_real_escape_string($_POST['todo']);
$email=mysql_real_escape_string($_POST['email']);
$fname=mysql_real_escape_string($_POST['fname']);
$lname=mysql_real_escape_string($_POST['lname']);
if(isset($todo) and $todo=="post"){
$status = "OK";
$msg="";
}
if(!isset($username) OR strlen($username) <3){
$msg=$msg."Username should be equal to or more than 3 characters long<BR/>";
$status= "NOTOK";
}
if(mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")or die (mysql_error ()))){
$msg=$msg."Username already exists. Please try another one<BR/>";
$status= "NOTOK";}
if ( strlen($password) < 3 ){
$msg=$msg."Password must be more than 3 charactors long<BR/>";
$status= "NOTOK";
}
if ( $password <> $password2 ){
$msg=$msg."Passwords are not identical.<BR/>";
$status= "NOTOK";
}
if($status="NOTOK"){
echo "$msg<br/><input type='button' value='Retry' onClick='history.go(-1)'>";
}
else {
if(mysql_query("insert into users(username,password,email,fname,lname) values('$username','$password','$email','$fname','$lname')")or die (mysql_error ())){
echo "Welcome, You have successfully signed up";
}
else {
echo "Database Problem, please contact Site admin";
}
}
?>
</body>
</html>
You're missing an = on the line
if($status="NOTOK"){
It will assign the "NOTOK" to the $status and be boolean true.