show data in search without using submit button - php

Does anyone know how to search and show data without using submit button..because thats the only function i need to finish my code..can anyone help me? I dont want to use submit button cause it refreshes the page...I have the code for search already I need a function that after I search the data will show in the textboxes without using submit button.
php code:
<?php
if($_GET){
include('include/connect.php');
$batchcode = $_GET['code'];
$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app WHERE batchcode = '".$batchcode."' ");
if($sql) {
while($rows = mysql_fetch_array($sql)){
$aic[] = $rows['aic'];
$name[] = $rows['name'];
$address[] = $rows['address'];
}
}
}else{
$aic = array(NULL,NULL,NULL,NULL);
$name = array(NULL,NULL,NULL,NULL);
$address = array(NULL,NULL,NULL,NULL);
}
?>
html code:
<html>
<head>
<title>test</title>
</head>
<body>
<form action="" method="get">
Search Batchcode:<input type="text" name="code" id="query" /><br />
<table>
<tr>
<td>
aic: <br />
<input type="text" name="optA1" value="<?php if(empty($aic[0])){$aic[0] = array(NULL);}else{echo $aic[0];} ?>" /> <br />
<input type="text" name="optA2" value="<?php if(empty($aic[1])){$aic[1] = array(NULL);}else{echo $aic[1];} ?>" /> <br />
<input type="text" name="optA3" value="<?php if(empty($aic[2])){$aic[2] = array(NULL);}else{echo $aic[2];} ?>" /> <br />
<input type="text" name="optA4" value="<?php if(empty($aic[3])){$aic[3] = array(NULL);}else{echo $aic[3];} ?>" /> <br />
</td>
<td>
Name List: <br />
<input type="text" name="optB1" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" /> <br />
<input type="text" name="optB2" value="<?php if(empty($name[1])){$name[1] = array(NULL);}else{echo $name[1];} ?>" /> <br />
<input type="text" name="optB3" value="<?php if(empty($name[2])){$name[2] = array(NULL);}else{echo $name[2];} ?>" /> <br />
<input type="text" name="optB4" value="<?php if(empty($name[3])){$name[3] = array(NULL);}else{echo $name[3];} ?>" /> <br />
</td>
<td>
Address: <br />
<input type="text" name="optC1" value="<?php if(empty($address[0])){$address[0] = array(NULL);}else{echo $address[0];} ?>" /> <br />
<input type="text" name="optC2" value="<?php if(empty($address[1])){$address[1] = array(NULL);}else{echo $address[1];} ?>" /> <br />
<input type="text" name="optC3" value="<?php if(empty($address[2])){$address[2] = array(NULL);}else{echo $address[2];} ?>" /> <br />
<input type="text" name="optC4" value="<?php if(empty($address[3])){$address[3] = array(NULL);}else{echo $address[3];} ?>" /> <br />
</td>
</form>
</body>
</html>
script code:
<!--search function code-->
<script type="text/javascript">
$(document).ready(function(){
$("#query").autocomplete({
source : 'search.php',
select : function(event,ui){
$("#query").html(ui.item.value);
}
});
});
</script>
search.php page code:
<?php
$q = $_GET['term'];
mysql_connect("localhost","root","");
mysql_select_db("test");
$query = mysql_query("SELECT DISTINCT batchcode FROM tb_app WHERE batchcode LIKE '$q%'");
$data = array();
while($row = mysql_fetch_array($query)){
$data[]=array('value'=>$row['batchcode']);
}
echo json_encode($data);
?>

In your html you need to create a link or a simple button and give it a class or id.
Then in javascript add click event listener and perform an ajax call.
search here
And in your javascript
$( "#mysubmit" ).click(function() {
// read query field value
// Perform ajax call
});
Hope this is going to help you.

You can also do:
$( "#query" ).change(function() {
var sendMe = $.post("search.php");
sendMe.done(function(data) {
$( "#query" ).html(data);
});
});

Related

PHP/MYSQL:Updated data isn't appearing on the table

Using php, i created an edit option that basically lets u change the data in the record. However, after clicking on submit, the data doesn't get saved on the table nor do i get any errors.. I am quite confused. Here r my codes:
<html>
<body>
<?php
include('db.php');
if (isset($_GET['Id'])) {
$Id=$_GET['Id'];
if (isset($_POST['submit'])) {
$Fname=$_POST['Fname'];
$Lname=$_POST['Lname'];
$Age=$_POST['Age'];
$Nationality=$_POST['Nationality'];
$PhoneNumber=$_POST['PhoneNumber'];
$Email=$_POST['Email'];
$query3=mysql_query("UPDATE `students` SET `Fname`='$Fname',`Lname`='$Lname',`Age`='$Age',`Nationality`='$Nationality',`PhoneNumber`='$PhoneNumber',`Email`='$Email' WHERE `Id`='$Id'");
if($query3) {
header('location:index1.php');
}
}
$query1=mysql_query("select * from students where Id='$Id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="">
First Name:<input type="text" name="name" value="<?php echo $query2['Fname']; ?>" /><br />
Last Name:<input type="text" name="name" value="<?php echo $query2['Lname']; ?>" /><br />
Age:<input type="text" name="age" value="<?php echo $query2['Age']; ?>" /><br />
Nationality:<input type="text" name="name" value="<?php echo $query2['Nationality']; ?>" /><br />
Phone Number:<input type="text" name="name" value="<?php echo $query2['PhoneNumber']; ?>" /><br />
Email:<input type="text" name="name" value="<?php echo $query2['Email']; ?>" /><br />
<br />
<input type="submit" name="submit" value="update" />
</form>
<?php
}
?>
</body>
</html>

getting id from post request

i'm trying to make CMS with PHP
i need to get the if from post request
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
how can i make this input's value = id
<input type="hidden" name="id" />
on control page
public function Update()
{
/*
1-get data into variables
2-validation
3-Database
*/
if(isset($_GET['id']) && (int)$_GET['id']>0)
{
$id = (int)$_GET['id'];
$user = $this->videoModel->Get_By_Id($id);
print_r($user);
echo
'
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>
';
}
else
{
if(isset($_POST['addVideo']))
{
$id = $_POST['id'];
echo "5ara" ;
$title = $_POST['title'];
$desc = $_POST['desc'];
$url = $_POST['ytl'];
//Validation
$vid = $this->getVid($url); //video id -_-
$data = array(
'title' => $title,
'desc' => $desc,
'vid' => $vid
);
if($this->videoModel->Update($id,$data))
{
System::Get('tpl')->assign('message','User Updated');
System::Get('tpl')->draw('success');
}
else
{
System::Get('tpl')->assign('message','Error Updating User');
System::Get('tpl')->draw('error');
}
}
else
{
System::Get('tpl')->assign('message','NO USER CHOSEN');
System::Get('tpl')->draw('error');
}
}
}
Quite simple really. As you checked it exists and moved it into a variable you can just echo the $id into the value attribute of that input tag.
<input type="hidden" name="id" value="' . $id . '" />
Using your code:
echo '
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" value="' . $id . '" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>';
Define the VALUE attribute :
<input type="hidden" name="id" value="ID_VALUE"/>
OR if you have id in varible than use below code :
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
OR
As you are using the HTML in PHP than:
<input type="hidden" name="id" value="'.$id.'"/>
ID_Value should be your id like 1,2,3..etc.
And Get the id on Action page :
$id = $_POST['id'];
Apart from this you have mention POST in form method and on your
action page you are trying to get the values using GET method,
which is wrong.
Edited due to question edit.
<input type="hidden" name="id" value="'.$id.'" />
You echo a big block of text, this way you can concatenate the $id by stopping block output, including $id and resuming block output.

my code wont update after search

can anyone help me find the code thats causing my code not to work? my code wont update... ive been debuging this code for 3hours already stil cant fix it :(...i need your help guys.
php code:
<?php
if(isset($_GET['gogo'])){
include('include/connect.php');
$batchcode = $_GET['code'];
$sql = mysql_query("SELECT * FROM score WHERE batchcode = '".$batchcode."' ");
if($sql) {
while($rows = mysql_fetch_array($sql)){
$id[] = $rows['id'];
$name[] = $rows['name'];
$score1[] = $rows['score1'];
$score2[] = $rows['score2'];
$other_qual[] = $rows['score3'];
$interview[] = $rows['score4'];
$total[] = $rows['total'];
}
}
}
?>
<?php
if(isset($_POST['update'])){
include('include/connect.php');
//1
$u1id = $_POST['id1'];
$u1name = $_POST['name1'];
$u1score1 = $_POST['optA1'];
$u1score2 = $_POST['optB1'];
$u1other_qual = $_POST['other_qual1'];
$u1interview = $_POST['interview1'];
$u1total = $_POST['total1'];
//2
$u2id = $_POST['id2'];
$u2name = $_POST['name2'];
$u2score1 = $_POST['optA2'];
$u2score2 = $_POST['optB2'];
$u2other_qual = $_POST['other_qual2'];
$u2interview = $_POST['interview2'];
$u2total = $_POST['total2'];
//1
mysql_query("UPDATE score SET score1='$u1score1', score2='$u1score2', total='$u1total' WHERE id='$u1id'");
//2
mysql_query("UPDATE score SET score1='$u2score1', score2='$u2score2', total='$u2total' WHERE id='$u2id'");
header("Location: index.php");
}
?>
html code:
<form method="get">
<form method="post">
Search batchcode: <input type="text" name="code" id="query" /><input type="submit" value="Go" name="gogo" /><br />
<table>
<tr>
<td>
ID: <br />
<input type="text" name="id1" value="<?php if(empty($id[0])){$id[0] = array(NULL);}else{echo $id[0];} ?>" readonly /> <br />
<input type="text" name="id2" value="<?php if(empty($id[1])){$id[1] = array(NULL);}else{echo $id[1];} ?>" readonly /> <br />
</td>
<td>
Name: <br />
<input type="text" name="name1" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" readonly /> <br />
<input type="text" name="name2" value="<?php if(empty($name[1])){$name[1] = array(NULL);}else{echo $name[1];} ?>" readonly /> <br />
</td>
<td>
Score 1: <br />
<input type="text" name="optA1" value="<?php if(empty($score1[0])){$score1[0] = array(NULL);}else{echo $score1[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optA2" value="<?php if(empty($score1[1])){$score1[1] = array(NULL);}else{echo $score1[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Score 2: <br />
<input type="text" name="optB1" value="<?php if(empty($score2[0])){$score2[0] = array(NULL);}else{echo $score2[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optB2" value="<?php if(empty($score2[1])){$score2[1] = array(NULL);}else{echo $score2[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Other Qualification: <br />
<input type="text" name="other_qual1" value="<?php if(empty($other_qual[0])){$other_qual[0] = array(NULL);}else{echo $other_qual[0];} ?>" readonly /> <br />
<input type="text" name="other_qual2" value="<?php if(empty($other_qual[1])){$other_qual[1] = array(NULL);}else{echo $other_qual[1];} ?>" readonly /> <br />
</td>
<td>
Interview: <br />
<input type="text" name="interview1" value="<?php if(empty($interview[0])){$interview[0] = array(NULL);}else{echo $interview[0];} ?>" readonly /> <br />
<input type="text" name="interview2" value="<?php if(empty($interview[1])){$interview[1] = array(NULL);}else{echo $interview[1];} ?>" readonly /> <br />
</td>
<td>
Total: <br />
<input type="text" name="total1" value="<?php if(empty($total[0])){$total[0] = array(NULL);}else{echo $total[0];} ?>" readonly onKeyUp="optTotal1()" /> <br />
<input type="text" name="total2" value="<?php if(empty($total[1])){$total[1] = array(NULL);}else{echo $total[1];} ?>" readonly onKeyUp="optTotal2()" /> <br />
</td>
</tr>
</table>
<input type="submit" value="update" name="update" />
</form>
</form>
You cannot do a GET and POST at the same time. Use one or the other..
In your HTML remove the <form method="get"> and the corresponding </form> and just use POST. (<form method="post">)
See this: Post and get at the same time in php
So then in your PHP, change GET to POST like so:
if(isset($_POST['gogo'])){
include('include/connect.php');
$batchcode = $_POST['code'];
$sql = mysql_query("SELECT * FROM score WHERE batchcode = '".$batchcode."' ");
...
EDIT:
Or alternatively, you could keep your php code the same the way you have it and just make it 2 seperate forms in your HTML,.. The search form using GET and the other form using POST
So HTML would be this:
<form method="get">
Search batchcode: <input type="text" name="code" id="query" /><input type="submit" value="Go" name="gogo" /><br />
</form>
<form method="post">
<table>
<tr>
<td>
ID: <br />
<input type="text" name="id1" value="<?php if(empty($id[0])){$id[0] = array(NULL);}else{echo $id[0];} ?>" readonly /> <br />
<input type="text" name="id2" value="<?php if(empty($id[1])){$id[1] = array(NULL);}else{echo $id[1];} ?>" readonly /> <br />
</td>...
...
</form>
If your code won't update then it is very likely you are updating the wrong files. Make sure that you are updating the files on the server or even better updating the files on your local drive and then uploading them to the server.
Check that you have permissions to upload to the correct place too. Might be that your uploads are failing because of bad permissions.

Update query not working in php

there are already values inside the texboxes which i filtered inside the table(score) but i wanna update it by changing the values inside on it and click update.
Can anyone help with my code..i wanna update the values inside the (texboxes) but my code wont work can anyone help me locate the code that messing with the program?
php code:
<?php
if(isset($_POST['update'])){
//1
$u1id = $_POST['id1'];
$u1name = $_POST['name1'];
$u1score1 = $_POST['optA1'];
$u1score2 = $_POST['optB1'];
$u1other_qual = $_POST['other_qual1'];
$u1interview = $_POST['interview1'];
$u1total = $_POST['total1'];
//2
$u2id = $_POST['id2'];
$u2name = $_POST['name2'];
$u2score1 = $_POST['optA2'];
$u2score2 = $_POST['optB2'];
$u2other_qual = $_POST['other_qual2'];
$u2interview = $_POST['interview2'];
$u2total = $_POST['total2'];
//1
mysql_query("UPDATE score SET score1='$u1score1', score2='$u1score2', total='$u1total' WHERE id='$u2id'");
//2
mysql_query("UPDATE score SET score1='$u2score1', score2='$u2score2', total='$u2total' WHERE id='$u2id'");
}
?>
html code:
<form method="post" id="frm" name="frm" action="" />
<table>
<tr>
<td>
ID: <br />
<input type="text" name="id1" value="<?php if(empty($id[0])){$id[0] = array(NULL);}else{echo $id[0];} ?>" readonly /> <br />
<input type="text" name="id2" value="<?php if(empty($id[1])){$id[1] = array(NULL);}else{echo $id[1];} ?>" readonly /> <br />
</td>
<td>
Name: <br />
<input type="text" name="name1" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" readonly /> <br />
<input type="text" name="name2" value="<?php if(empty($name[1])){$name[1] = array(NULL);}else{echo $name[1];} ?>" readonly /> <br />
</td>
<td>
Score 1: <br />
<input type="text" name="optA1" value="<?php if(empty($score1[0])){$score1[0] = array(NULL);}else{echo $score1[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optA2" value="<?php if(empty($score1[1])){$score1[1] = array(NULL);}else{echo $score1[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Score 2: <br />
<input type="text" name="optB1" value="<?php if(empty($score2[0])){$score2[0] = array(NULL);}else{echo $score2[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optB2" value="<?php if(empty($score2[1])){$score2[1] = array(NULL);}else{echo $score2[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Other Qualification: <br />
<input type="text" name="other_qual1" value="<?php if(empty($other_qual[0])){$other_qual[0] = array(NULL);}else{echo $other_qual[0];} ?>" readonly /> <br />
<input type="text" name="other_qual2" value="<?php if(empty($other_qual[1])){$other_qual[1] = array(NULL);}else{echo $other_qual[1];} ?>" readonly /> <br />
</td>
<td>
Interview: <br />
<input type="text" name="interview1" value="<?php if(empty($interview[0])){$interview[0] = array(NULL);}else{echo $interview[0];} ?>" readonly /> <br />
<input type="text" name="interview2" value="<?php if(empty($interview[1])){$interview[1] = array(NULL);}else{echo $interview[1];} ?>" readonly /> <br />
</td>
<td>
Total: <br />
<input type="text" name="total1" value="<?php if(empty($total[0])){$total[0] = array(NULL);}else{echo $total[0];} ?>" readonly onKeyUp="optTotal1()" /> <br />
<input type="text" name="total2" value="<?php if(empty($total[1])){$total[1] = array(NULL);}else{echo $total[1];} ?>" readonly onKeyUp="optTotal2()" /> <br />
</td>
</tr>
</table>
<input type="submit" value="update" />
</form>
It's in your submit button:
<input type="submit" value="update" />
You have given it a value, but not a name. If you change it to:
<input type="submit" name="update" value="yespleasedososir" />
it will end up in your post var
A few thoughts: 1. Have you echoed out your sql statements to see what you are getting? 2. Try add the tilde symbol before and after each column name...like score1 = 'whatever'. 3. You should really be using the mysqli statemnts.

php fetch your details from the database via email

I want to make a text field and button that will allow the user to fetch his details on the text fields instead of writting his details every time he wants to make a new reservation.
like in this picture:
http://oi41.tinypic.com/23ie70j.jpg
I tried to make this but with my code but gives me double forms one with the details and one without.
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$q = "SELECT * FROM tabe WHERE the_email = '$email'";
$run = mysql_query($q );
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}}
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
//limit the query to one entry :)
$q = "SELECT * FROM tabe WHERE the_email = '$email' LIMIT 1";
$run = mysql_query($q );
//check if email is registered
if(mysql_num_rows($run)>0)
{
//display filled up form
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}
}
//display blank form
else{
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<?php
}
}
?>
You're getting 2 forms because you're echoing one form if $_POST['submit'] is set and then another one regardless of anything. Print the second form only if $_POST['submit'] is not set. Since your code is so poorly written I will just give you an example:
if(isset($_POST['submit'])){
PRINT FETCHED FORM
}else{
PRINT EMPTY FORM
}
This, however, is not the "right" way to go. What people actually do is have variables null'd at start and then fill them up with data if there's a request and have a single form written in the file with input values as those variables.

Categories