How to fill texboxes from database when an onClick event happens - php

I want my php file to fill textboxes with information from the database according to the selected value of a select box tag. And every time the selection changes the textboxes should be re-filled accordingly.
The problem is that when I call a javascript method which contains php code the page is reloaded and some initial values collected by $_POST[] when the page was loaded for the first time are lost.
I want to find a way such that those initial values collected by $_post[] are conserved even after the page re-executes the php. How can I solve this problem.
<script type="text/javascript">
function displaymessagespatient()
{
<?php
function docmsg()
{
$title = $_POST["title"];
$conn=mysqli_connect("localhost","root","","askthedoctor");
$sql1="select patient_text from messages where title='".$title."';";
$sql2="select doctor_text from messages where title='".$title."';";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2);
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
return $row1[0];
}
function patmsg()
{
$title = $_POST["title"];
$conn=mysqli_connect("localhost","root","","askthedoctor");
$sql1="select patient_text from messages where title='".$title."';";
$sql2="select doctor_text from messages where title='".$title."';";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2);
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
return $row2[0];
}
?>
document.getElementById("answer").innerHTML=docmsg();
document.getElementById("question").innerHTML=patmsg();
}
</script>
</head>
<body>
<?php
if( isset($_POST['username']))
{
$username=$_POST["username"];
$password=$_POST["password"];
$password=md5($password);
$conn=mysqli_connect("localhost","root","","askthedoctor");
$sql="select password from login where username='".$username."';";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
if($row[0]!=$password)
{
echo "The username or password that you entered are incorrect!";
echo "<br/>";
echo "Go back to login";
die();
}
$sql1="select userprivileges from login where username='".$username."';";
$sql2="select image_path from registration1 where id=(select id from login where username='".$username."');";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2);
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
$imagepath=$row2[0];
}
if($row1[0]==2)
{
?>
<form id="patient" method="POST" enctype="multipart/form-data" action="login.php" >
<div id="header" class="class_header">
Sign Out
</div>
<div id="body" >
<br/>
<table class="table">
<tr>
<td><font color="white" ><h1 color="white" style="font-size:200%;" align="center">Welcome <?php echo $username;?></h1></font></td>
<td> <div id="box"><image height="65px" width="65px" src="<?php echo $imagepath; ?>"></div></td>
<tr>
</table>
</div>
<div id="separator" ></div>
<div id="separator" ></div>
<div id="bodyy" style="height: 60%; width: 60%;" class="div">
<br/><br/><br/>
<fieldset id="registration">
<table class="table">
<tr>
<th>Messages</th>
<?php
$sql="select title from messages where paitient_id=(select id from login where username='".$username."');";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result))
{
?>
<th>Problem Description</th>
<th>Doctor's Answer</th>
<th></th>
</tr>
<tr>
<td><select name="title">
<?php echo "<option value=\"mesazhi1\">".$row[0]."</option>";}?>
</select>
</td>
<td><textarea rows="4" col="50" id ="question" readonly> </textarea></td>
<td><textarea rows="4" col="50" id ="answer" readonly> </textarea></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="openmessage" value="Display Selected Message" onClick="displaymessagespatient()"></td>
<td><input type="button" name="btnSubmit" value="Ask a new question" ></td>
</tr>
</table>
</fieldset>
</div>
</div>
</form>
<?php } ?>
</body>

Related

How to add a record to a database using id instead of changing them (javascript and php)?

How do I add a few records to a database using the id’s I had listed in phpMyAdmin. The problem is once I entered the id , it display the database but when I entered another id , it took out and change the value of the first id database I entered and replace it with the second id. What I want is to add the database one by one through each click but instead of replacing it, I would like to add them below after the first ID I entered earlier. This is what I’m working on so far.
<html>
<head>
<title>Search data by its ID</title>
</head>
<body>
<center>
<h1>Search a single DATA</h1>
<h2>Retrieve data from database</h2>
<div class="container">
<form action="" method="POST">
<input type="text" name="id" placeholder="Student ID" />
<input type="submit" name="search" value="Search By ID" />
</form>
<table border="2" id="newton">
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Returned Date</th>
</tr><br><br>
<?php
$connection = mysqli_connect("localhost","root", "");
$db = mysqli_select_db($connection,"myfirstdb");
if(isset($_POST['search']))
{
$id = $_POST['id'];
$query = "SELECT * FROM `table3` where id = '$id'";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td>
<?php echo $row ['product_name']; ?> </td>
<td>
<?php echo $row ['quantity']; ?> </td>
<td>
<?php echo $row ['returned_date']; ?> </td>
</tr>
<?php
}
}
?>
</table>
</form>
</div>
</center>
</body>
</html>
If I understand your problem correctly, the following code can solve your problem. Although the student ID has nothing to do with the name of the product !!
you can use a session for this problem.
<html>
<head>
<title>Search data by its ID</title>
</head>
<body>
<center>
<h1>Search a single DATA</h1>
<h2>Retrieve data from database</h2>
<div class="container">
<form action="" method="POST">
<input type="text" name="id" placeholder="Student ID" />
<input type="submit" name="search" value="Search By ID" />
</form>
<table border="2" id="newton">
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Returned Date</th>
</tr><br><br>
<?php
$connection = mysqli_connect("localhost","root", "");
$db = mysqli_select_db($connection,"myfirstdb");
session_start();
if (!isset($_SESSION['id'])) {
$_SESSION['id'] = array();
}
if(isset($_POST['search']))
{
$id = $_POST['id'];
array_push($_SESSION['id'],$id);
$_SESSION['id'] = array_unique($_SESSION['id']);
$id = implode(',',$_SESSION['id']);
$query = "SELECT * FROM `table3` where id in ($id)";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td>
<?php echo $row ['product_name']; ?> </td>
<td>
<?php echo $row ['quantity']; ?> </td>
<td>
<?php echo $row ['returned_date']; ?> </td>
</tr>
<?php
}
}
?>
</table>
</form>
</div>
</center>
</body>
</html>

PHP: A HTML hidden input value generates an error upon querying mysql

I'm working on a webpage where I allow users to edit their car information. In the mainlining, there is an edit button (input - type text with a hidden key value) where it takes the user to this "edit car info" page. Initially, once the page is opened for the first time, this hidden value is used to query the database, retrieve original information and and set them as placeholders for the field. The user can write information in the input field then press the "submit edit" button which then updates the row in the database table. However, I get an error that the name of the hidden value is undefined. I don't understand how it can be undefined for the update query when it was working just fine for the select query. Can anyone shed a light on this? What should I do? This is a picture of the errors:
This is the mainlanding code: (hidden value is set here)
<?php
$mysqli= new mysqli("localhost", "root","","Car_registration");
if(empty($_SESSION)) // if the session not yet started
session_start();
if(isset($_SESSION['username'])) { // if user already logged in
header("location: mainlanding_user.php"); //send to homepage
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Car Registration: User's Mainlanding </title>
<link href="css/style3.css" rel="stylesheet">
</head>
<body>
<header>
<h1>Account Information</h1>
<img id="img1" src= "image/car.jpg" alt ="car image">
</header>
<nav id='nav'>
<form action="logout.php">
<input type="submit" value=" Logout " id="button">
</form>
</nav>
<h2>Profile </h2>
<div class='container1'>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from driver where username='".$_SESSION['logged_username']."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc() ){
echo "<div id='container'>" ;
echo "<dl> <dt>First Name</dt> <dd>".$row['Fname'];
echo "</dd> <br> <dt>Last name</dt><dd>".$row['Lname'];
echo "</dd> <br> <dt>License Number</dt><dd>".$row['license_no'];
echo "</dd> <br> <dt>Age</dt><dd>".$row['Age'];
echo "</dd> <br> <dt>Birthday</dt><dd>".$row['bdate'];
echo "</dd> <br> <dt>City</dt><dd>".$row['City'];
echo "</dd></dl>";
echo "</div>";
$license_no = $row['license_no']; //used for finding cars
}
?>
<div class="align-me">
<div class="form-wrapper" action="search_plate_no.php">
<form class="center">
<input class="input-fields" name="search" type="text" placeholder="Search a plate number">
<input class="input-fields submit" name="find" type="submit" value="Search">
</form>
</div>
</div>
<h3> Registered Cars </h3>
<div class='container2'>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);
echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while ($temp = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $temp['Plate_no']; ?></td>
<td><?php echo $temp['license_no']; ?></td>
<td><?php echo $temp['Car_type']; ?></td>
<td><?php echo $temp['Fines']; ?></td>
<td><?php echo $temp['city']; ?></td>
<td>
<form action = "edit_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="edit" value="Edit">
</form>
</td>
<td>
<form action = "delete_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="delete" value="Delete">
</form>
</td>
</tr>
<?php
}
?>
</table>
</div>
<form action="register_car.php">
<input type="submit" value=" Register Car " id="button2">
</form>
<footer>
<h4> All rights belong to Car Registration Inc. </h4>
<img id="img3" src= "image/license.png" alt ="license plates image">
</footer>
</body>
</html>
Edit car page: (Error is generated here)
<!DOCTYPE html>
<html>
<head>
<title> Edit Car Information Page </title>
<link href="css/style2.css" rel="stylesheet">
</head>
<body>
<div class="container">
<header>
<h1>Edit Car Information </h1>
<img id="img1" src= "image/register.png" alt ="Registration image">
</header>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$plate_no= $_POST["id"]; //This line causes an error
$_SESSION['plateNo'] = $plate_no;
$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc()){
$plate_no = $row['Plate_no'];
$car_type = $row['Car_type'];
}
?>
<main>
<h2> You can only edit the following information: </h2>
<form action="" method="post">
<label for="car_type_input">Car Type:</label>
<input type="text" placeholder="<?php echo $car_type?>" id="car_type_input" name="car_type_input"><br><br>
<div class="vertical-center">
<input type="submit" value=" Submit Edit " name="button1" id="button1">
</div>
</form>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
if( isset($_POST['button1']) ){ //If user changed field, take value. If not, keep old value.
if( !empty($_POST['car_type_input']) ){ //If there is user input
$car_type_2 = $_POST['car_type_input'];
$query= "update cars set Car_type='".$car_type_2."' WHERE Plate_no='".$_SESSION['plateNo']."'";
}
if ($mysqli->query($query))
echo "Fields updated successfuly!";
else
echo "Update Fields Failed!";
}
?>
</main>
<footer>
<h3> All rights belong to Car Registration Inc. </h3>
<img id="img3" src= "image/license.png" alt ="license plates image">
</footer>
</div>
</body>
</html>
Use $plate_no= $_POST['id']; instead of $plate_no= $_POST["id"];
Here why you close the while loop ??
while ($temp = $result->fetch_assoc()){
?>
and here too
<?php
}
Try this:
print"<h3> Registered Cars </h3>
<div class='container2'>";
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);
echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while ($temp = $result->fetch_assoc())
{
print"
<tr>
<td><?php echo $temp['Plate_no']; ?></td>
<td><?php echo $temp['license_no']; ?></td>
<td><?php echo $temp['Car_type']; ?></td>
<td><?php echo $temp['Fines']; ?></td>
<td><?php echo $temp['city']; ?></td>
<td>
<form action = "edit_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="edit" value="Edit">
</form>
</td>
<td>
<form action = "delete_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="delete" value="Delete">
</form>
</td>
</tr> ";
}
print"</table>
</div>";
you are not sending id that's because error appears use this code to check if id exists first:
$plate_no='';
$car_type = '';
if(isset($_POST["id"])){
$plate_no= $_POST["id"]; //This line causes an error
$_SESSION['plateNo'] = $plate_no;
$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc()){
$plate_no = $row['Plate_no'];
$car_type = $row['Car_type'];
}
}

Cannot delete row in database using PHP

I can't seem to delete row in database by id in php
I think the the id is not passed to the $_POST['delete']
however, the popup "Your data is deleted" is displayed, but the data is not deleted.
So I'm not sure where is the error in this code.
I also try to delete the data by its id
for example: Delete book where no='4';
and the code seems to run fine because the data is deleted in the database.
<html>
<script>
function confirmDelete() {
return confirm('Are you sure?');
}
</script>
<!DOCTYPE html>
<head>
<form action="test.php" method="POST">
<br><br><br>
<table bordercolor="#FFCC66" align="center" bgcolor="#FFFFFF">
<tr>
<th>No</th>
<th>Title</th>
<th>Author</th>
<th>Year</th>
<th>Donor's Name</th>
<th>Call Number</th>
<th>Date Received</th>
<th>Handled By</th>
<th></th>
<th></th>
</tr>
<?php
include ('config.php');
$view=mysqli_query($conn,"SELECT * FROM book");
?>
<?php while($v=mysqli_fetch_array($view)){ ?>
<tr>
<td>
<?php echo $v["no"];?>
</td>
<td>
<?php echo $v["title"];?>
</td>
<td>
<?php echo $v["author"];?>
</td>
<td>
<?php echo $v["year"];?>
</td>
<td>
<?php echo $v["donorname"];?>
</td>
<td>
<?php echo $v["callnum"];?>
</td>
<td>
<?php echo $v["datereceived"];?>
</td>
<td>
<?php echo $v["handledby"];?>
</td>
<td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
</tr>
<?php
} ?>
</tr>
</table>
<br><br>
</form>
</body>
</html>
<?php
if(isset($_POST['delete']))
{
include('config.php');
$no =$v["no"];
$d=mysqli_query($conn,"DELETE FROM `book` WHERE no='$no'");
if ($d)
{
echo "<script type='text/javascript'> alert('Your data is deleted!'); </script>";
echo"<meta http-equiv='Refresh' content='0' >";
}
else
{
echo "<script type='text/javascript'> alert('Your data cannot delete!'); </script>";
}
mysqli_close($conn);
}
?>
Change the submit element to
<td>
<input type="submit" name="delete" value="<?php echo $v['no'];?>" onclick="return confirmDelete('Are you sure?');" />
</td>
and
$no = $_POST["delete"];
Another solution si to add a hidden input with your value.
<td>
<?php echo $v["no"];?>
<input type="hidden" value="<?php echo $v["no"];?>" />
</td>
In your php you will find the value in $_POST['no']
This solution is better to pass multiple arguments in POST like a captcha or a confirmation (checkbox).
logic is not correct, while you press the delete button, all the data will be passed along with submitting because your tag is outside of the loop.
As my opinion, you should use ajax like functionality here, or follow this method.
<?php while($v=mysqli_fetch_array($view)){ ?>
<form action="test.php" method="POST">
<tr>
<td>
<?php echo $v["no"];?>
<input type="hidden" value="<?php echo $v["no"];?>" name="no" >
</td>
<td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
</tr>
</form>
<?php } ?>
and in your post call use $no = $_POST['no']; instead of $no =$v["no"];

Post data that contains paths with ../../ gives empty $_POST array

Recently I moved my codeigniter website to a new server (goddady). Before this everything was working great with no problems. But now I started to get strange problems with post data, whenever I try to insert data that contains relative paths with dots (../../) and try to submit the form, I get an empty $_POST array. The strange thing is that this happens only with certain forms, not all of them. What could cause such problem?
Here is the form that causes problem:
<?php
if(isset($posts2) && count($posts2) == 1){
$posts2 = $posts2[0];
echo form_open_multipart('professors/update_biography/', array("id" => "professors_edit"));
echo form_hidden('posts2[id]', $posts2->id);
if(isset($user) && count($user) == 1){
$user = $user[0];
echo form_hidden('user[id]', $user->id);
echo form_hidden('user[role]', "Professor");
}
?>
<table class="admin_table">
<tr>
<th>
Биографија
</th>
<td>
<textarea name='posts2[biography]'><?php echo $posts2->biography; ?></textarea>
</td>
</tr>
<tr>
<th>
Биографија EN
</th>
<td>
<textarea name='posts2[biography_en]'><?php echo $posts2->biography_en; ?></textarea>
</td>
</tr>
<tr>
<th>
Cv
</th>
<td>
<p class="old">CV</p>
<input type="file" name='cv' id="pdf"></input>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type='submit' name='submit' value='Зачувај' />
</td>
</tr>
</table>
<?php
echo form_close();
?>
<div class="redButton" style="float:left; width: 150px;">
<?php
if(!isset($prof[0]->id)){ //da ne go prikazuva za profesor
echo anchor('professors/', 'Назад до професори');
}
?>
</div>
<?php
}
?>

How to call php function from html form action?

I want to call php function in form action and i want to pass id as a argument. What I am doing is, in html form database column values will be displayed in text boxes, If I edit those values and click 'update' button values in database should be updated and 'Record updated successfully'message should be displayed in same page. I tried below code but not working. Let me know the solution. Thanks in advance.
<html>
<head>
<link rel="stylesheet" type="text/css" href="cms_style.css">
</head>
<?php
$ResumeID = $_GET['id'];
$con = mysql_connect("localhost", "root", "");
mysql_select_db("engg",$con);
$sql="SELECT * from data WHERE ResumeID=$ResumeID";
$result = mysql_query($sql);
$Row=mysql_fetch_row($result);
function updateRecord()
{
//If(!isset($_GET['id']))
//{
$NameoftheCandidate=$_POST[NameoftheCandidate];
$TelephoneNo=$_POST[TelephoneNo];
$Email=$_POST[Email];
$sql="UPDATE data SET NameoftheCandidate='$_POST[NameoftheCandidate]', TelephoneNo='$_POST[TelephoneNo]', Email='$_POST[Email]' WHERE ResumeID=$ResumeID ";
if(mysql_query($sql))
echo "<p>Record updated Successfully</p>";
else
echo "<p>Record update failed</p>";
while ($Row=mysql_fetch_array($result)) {
echo ("<td>$Row[ResumeID]</td>");
echo ("<td>$Row[NameoftheCandidate]</td>");
echo ("<td>$Row[TelephoneNo]</td>");
echo ("<td>$Row[Email]</td>");
} // end of while
} // end of update function
?>
<body>
<h2 align="center">Update the Record</h2>
<form align="center" action="updateRecord()" method="post">
<table align="center">
<input type="hidden" name="resumeid" value="<? echo "$Row[1]"?>">
<? echo "<tr> <td> Resume ID </td> <td>$Row[1]</td> </tr>" ?>
<div align="center">
<tr>
<td> Name of the Candidate</td>
<td><input type="text" name="NameoftheCandidate"
size="25" value="<? echo "$Row[0]"? >"></td>
</tr>
<tr>
<td>TelephoneNo</td>
<td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[1]"?>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>">
</td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td>
</tr>
</div>
</table>
</form>
</body>
</html>
try this way
HTML
<form align="center" action="yourpage.php?func_name=updateRecord" method="post">
PHP
$form_action_func = $_POST['func_name'];
if (function_exists($form_action_func)) {
updateRecord();
}
write form action="" and then write your php code as below
note : use form method as get
<?php
if(isset($_GET['id']))
{
call your function here
}
?>
in function access all values using $_GET['fieldname']
simple way make your "Submit " and "Update" action performed on same page then
if(isset($_POST['update']))
{
//perform update task
update($var1,var2,$etc); // pass variables to function
header('Location: http://www.example.com/');// link to your form
}
else if(isset($_POST['submit']))
{
//perform update task
submit($var1,$var2,$etc);// pass variables to function
header('Location: http://www.example.com/'); // link to next page after submit successfully
}
else
{
// display form
}

Categories