I am using this autocomplete tut: http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/ .
The jQuery aren't displaying. You can actually click the grey line (shown in picture) and it will load everything. I just can't see the names for some reason.
I thought it could be a css problem, but I couldn't find anything relevant.
<div class="guestinfo">
<form action='' method="POST">
<div class="ui-widget">
<div class="existingguest"><label>Exisiting Guest</label>
<input type="text" class="auto" name="guests" id="guests"/></div>
</div>
<div class="existingguestinfo">
<div><label>First Name</label><input readonly="readonly" type="text" id="firstname" name="firstname"/></div>
<div><label>Last Name</label><input readonly="readonly" type="text" id="lastname" name="lastname"/></div>
<script>
$(function() {
$('.auto').val("");
$(".auto").autocomplete({
source: "classes/autocomplete_guests.php",
minLength: 1,
select: function(event, ui) {
$('#firstname').val(ui.item.fname);
$('#lastname').val(ui.item.lname);
$('#address').val(ui.item.address);
$('#phone').val(ui.item.phone);
}
});
});
</script>
<!--<div><label>Add New Guest</label></div>-->
<div><label>Address</label><input readonly="readonly" type="text" id="address" name="address"/></div>
<div><label>Phone</label><input readonly="readonly" type="text" id="phone" name="phone"/></div>
</br></div>
</form>
</div> <!-- end guestinfo div -->
autocomplete_guests.php
<?php
//open connection
require_once('../config/db.php');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$return_arr = array();
/* If connection to database, run sql statement. */
if ($con)
{
$fetch = mysqli_query($con, "SELECT * FROM guests WHERE lname like '%".mysqli_real_escape_string($con, $_GET['term'])."%'");
/* Retrieve and store in array the results of the query.*/
while ($row = mysqli_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['lname'] = $row['lname'];
$row_array['fname'] = $row['fname'];
$row_array['gender'] = $row['gender'];
$row_array['address'] = $row['address'];
$row_array['city'] = $row['city'];
$row_array['state'] = $row['state'];
$row_array['phone'] = $row['phone'];
$row_array['email'] = $row['email'];
$row_array['dob'] = $row['dob'];
$row_array['zip'] = $row['zip'];
array_push($return_arr,$row_array);
}
}
/* Free connection resources. */
mysqli_close($con);
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
?>
You should
1) write script tags like <script type="text/javascript">
2) Put your javascript at the bottom of the page and not half way through the form... (?!!)
3) Add a document ready to that function so that it executes when the page finished loading.
$( document ).ready(function() {
4) Make sure you have included all the required jquery files: Jquery Core, Jquery UI, etc.
5) Provide some CSS if you think that is the problem??
EDIT:
Ignore 3. Ive just looked at the plugin documentation.
Just try these lines inside before ur Jquery script
====sorry if am wrong =====
Related
I'm trying to get multiple checked checkbox value data added to mysql with php without reloading the form page and show confirmation message in div on form page. At the moment, showing on div works but data is not being sent.
I already have done another similar one which I had almost the same code that had the input as text area so I changed that part and it works but the this one is not working. Could anyone give me help here?
form is in vote.php:
<html>
<form action="vote_received.php" method="post" target="" id="vote-submit">
<input type="hidden" name="recieved-date" id="todayDate" />
<input type="hidden" name="user-occup" id="user-occup" value="<?php $_SESSION['occupation']; ?>" />
<input type="checkbox" name="voting[]" value="How might we improve driver facilities such as lunch rooms or break rooms?" id="voting_1">
<label for="voting_1">How might we improve driver facilities such as lunch rooms or break rooms?</label>
<input type="checkbox" name="voting[]" value="How might be increase driver security at night time?" id="voting_2">
<label for="voting_2">How might be increase driver security at night time?</label>
<input type="checkbox" name="voting[]" value="How might we change the on-call communication with management?" id="voting_3">
<label for="voting_3">How might we change the on-call communication with management?</label>
<input type="checkbox" name="voting[]" value="How might we enhance the passenger conflict management system?" id="voting_4">
<label for="voting_4">How might we enhance the passenger conflict management system?</label>
<br><br>
<input type="submit" name="submit" value="Submit" class="btn align-right"></form>
<script>
$("#vote-submit").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $(this),
$messages = $("input[name='voting[]']"),
$occup = $form.find('input[name="user-occup"]'),
$submit = $form.find('button[type="submit"]'),
message_value = $messages.val(),
url = $form.attr('action');
var posting = $.post(url, { submission : message_value});
posting.done(function(data) {
/* Put the results in a div */
$("#vote_success").html('<h2>THANK YOU!</h2><p>Thank you for your voting. Meeting invitations will be send out on December 7th, 2017.');
/* Hide form */
$form.hide();
});
});
</script>
</html>
the vote_received.php is:
<?php
session_start();
if(isset($_POST['vote-submit'])) {
$voteArray=$_POST['voting'];
$conn = mysqli_connect($servername, $username, $password, $database);
if(is_null($voteArray)) {
echo("<p>You didn't select any topic.</p>\n");
} else {
$N = count($voteArray);
for($i=0; $i < $N; $i++) {
$var1 = $voteArray[$i];
$jobTitle = $_SESSION['occupation'];
$sql = "INSERT INTO vote_response (occupation, voting,
created) VALUES('$jobTitle', '$var1', now())";
$success = mysqli_query($conn, $sql);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo $var1;
$conn->close();
}
}
}
?>
Thank you very much!
try to change this part of your code
var $form = $(this),
$messages = $("input[name='voting[]']"),
$occup = $form.find('input[name="user-occup"]'),
$submit = $form.find('button[type="submit"]'),
message_value = [],
url = $form.attr('action');
$.each($messages, function(idx, val){
message_value.push($(val).val());
});
var posting = $.post(url, { submission : message_value});
on further reading of your code, try to change this part of you code also:
if(isset($_POST['submission'])) {
$voteArray=$_POST['submission'];
been working on some database data calling into a .php file.
The php file contains an "Add" button, a "textarea" and an "submit" button.
I did added some J Query script to it to make the "textarea and submit" button to hide until "add" button is clicked, and both "textarea and submit" to hide when "submit" button is clicked making "add" button reappear.
Ever thing is working fine but only glitch is, the script is only working for first row in the table, leaving the rest of rows uneffected.
I think i should use a loop or something.. spent couple of hours but couldn't able to figure it out by myself.
my script goes as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$dbname = "the_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
$sql = "SELECT * FROM input";
$result = $conn->query($sql);
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.js">
</script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$().ready = function() {
$('#text').hide();
$('#textsubmit').hide();
$("#addanswer").click(function() {
$('#addanswer').hide();
$('#text').fadeIn('slow').focus();
$('#textsubmit').fadeIn('slow');
});
$('#text').blur(function(){
$('#text').hide();
$('#textsubmit').hide();
$('#addanswer').fadeIn('slow');
});
}();
});//]]>
</script>
</head>
<body>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<button class="addanswer" id="addanswer"><B>Add Answer</B></button>
<form name="answer" method="post" action="output.php">
<textarea type="text" class="text" name="text" required id="text" placeholder="Please type your question here.."></textarea>
<button type="submit" id="textsubmit" class="textsubmit"><B>Submit</B></button>
</form>
<?php }
} else {
echo "0 results";
}
$conn->close();
?>
</body>
Since you cannot have multiple occurrences of the same ID, you could do something like this.
<?php
foreach( $your_data as $index => $data ){
echo '<button class="addanswer" id="addanswer_'.$index.'"><B>Add Answer</B></button>';
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">'; // I dont think openning form from row to row would be nice!
echo '<textarea style="display:none;" type="text" class="text" name="text" required id="text_'.$index.'" placeholder="Please type your question here.."></textarea>';
echo '<button style="display:none;" onClick="addanswer('.$index.');" type="submit" id="textsubmit_'.$index.'" class="textsubmit"><B>Submit</B></button>';
echo '</form>';
}
... Other code stuff.
Now the each row is having a different ID because we have used $index variable. And also we pass the $index to the javascript function as well. So the javascript can do what ever based on the $index value.
You can have your javascript function, something like this.
<script type='text/javascript'>
function addanswer(index){
$('#addanswer_' + index).hide();
$('#text_' + index).fadeIn('slow').focus();
$('#textsubmit_' + index).fadeIn('slow');
}
</script>
Note: I havent checked this code by running it. I think you will get some understanding with this.
Thanks
Dear users with your help and guidance I have achieved so far. Thanks to the community here. As some users felt that it was a duplicate question - sorry I changed the expected result. With members guidance i think i can achieve this.
I have a form with a textarea, a combo, a text box and other elements. First 1) I enter address in the textarea
2)I select a pincode - which is populated from a table
3)when pincode is selected the next text field is populated by the same table used in point (2) above.
For this the page is refreshed with pincode and it display the place in the next text box.
Every think ok. But what i typed in the textarea and what i selected in the combo is refreshed to blank. I need to replace what i typed and selected.
The script used for collecting the pincode
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.pin_code.options[form.pin_code.options.selectedIndex].value;
self.location='addschool.php?pin_code=' + val ;
}
</script>
The php code below:
<form data-toggle="validator" role="form">
<div class="form-group textareawidth has-feedback">
<label for="address">Enter school address</label>
<textarea class="form-control" pattern = "^[_A-z0-9]{1,}$" maxlength="150" rows ="3" name = "saddress" id="address" placeholder="Enter address with out pincode" required></textarea>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
<span class="help-block with-errors"></span>
</div>
<div class="form-group textareawidth">
<label for="pin">Pincode - School:</label>
<?php
echo "<select class='form-control' id='pin' onchange=\"reload(this.form)\" name ='pin_code' name=pin_code value='' >";
echo "<option selected='selected'>Select Pincode </option>";
while($nt=mysqli_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[pin]>$nt[pin]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
</div>
<?php
$vplace="";
if(isset($_GET['pin_code']))
{
$temp=$_GET['pin_code'];
$quer="SELECT place FROM pincode where pin = $temp ";
$ex1=mysqli_query($dbcon,$quer) or die(mysql_error());
$count1=mysqli_num_rows($ex1);
if($count)
{
$row = mysqli_fetch_assoc($ex1);
$vplace = $row["place"];
}
else
{
echo '<script>';
echo 'alert("no such place found");';
echo '</script>';
}
}
?>
<div class="form-group textareawidth">
<label for="place">Place</label>
<?php
//echo "<input type ='text' class='form-control' name = 'splace' id='place' value =$vplace]>;";
echo "<p class='form-control-static'>$vplace</p>";
?>
</div>
How can I achieve this ? Thanks
According to your last comment I update the answer.
Here is an example of what you want:
in your form page (here is form.php) put the following code.
form.php:
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
$('#pin').change(function(e) {
var ajaxData = {
pinCode: $(this).val()
};
$.post('getPlace.php', ajaxData, function(response){
$('#place').val(response);
});
});
});
</script>
</head>
<body>
<form id="myform" name="frmForm" method="post" action="doAction.php">
<label for="address">Enter school address:</label><br>
<textarea id="address" name="txtAddress"></textarea><br>
<label for="pin">Pincode - School:</label><br>
<select id="pin" name="cmbPin">
<?php
$db = mysqli_connect('localhost','root','','testdb') or die(mysqli_connect_error());
$query = 'SELECT pin FROM pincode';
$result = mysqli_query($db,$query);
while($row = mysqli_fetch_array($result)){
echo "<option value=\"$row[pin]\">$row[pin]</option>" . PHP_EOL;;
}
?>
</select><br>
<label for="place">Place:</label><br>
<input id="place" name="txtPlace" type="text"><br>
<input id="submit" name="btnSubmit" type="submit" value="GO!">
</form>
</body>
</html>
And create an new .php file in the same directory called getPlace.php and put the php code that fetches place from your database.
getPlace.php:
<?php
if(isset($_POST['pinCode'])){
$pinCode = $_POST['pinCode'];
$db = mysqli_connect('localhost','root','','testdb') or die(mysqli_connect_error());
$query = "SELECT * FROM pincode WHERE pin = '$pinCode'";
$sql = mysqli_query($db, $query);
$result = mysqli_fetch_array($sql);
$place = $result['place'];
echo $place;
}
?>
for more information about jQuery and Ajax functions and parameters read the Documentation.
I am developing a PhoneGap application that gets some information from MySQL Database. I am struggling when I try to open a HTML page that contains two select input that need to be populated on page load, each one with data from two different tables. I don't why, but they are not getting populated. Please, any help will be very welcome.
HTML CODE
<div data-role="content">
<p></p>
<form id="cname" align="left" action="post" data-ajax="false" >
<label for "id">Employee's Name:</label><br/>
<select name="id" id="id"></select><br/>
<label for "job_id">Job's Name:</label><br/>
<select name="job_id" id="job_id"></select><br/>
<input type="hidden" name="latitued" id="latitued" value="">
<input type="hidden" name="longitude" id="longitude" value="" >
<input type="hidden" name="goo_map_api" id="goo_map_api" value="">
<input type="submit" value="Clock-In" id="enviar_in" data-inline="true">
</form>
</div
Jquery Script both SELECTS
<script type="text/javascript">
$(document).ready(function(e){
var items="";
$.getJSON("get_emp.php",function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.id+"'>"+item.fullName+"</option>";
});
$("#id").html(items);
});
});
</script>
<script type="text/javascript">
$(document).ready(function(e){
var items="";
$.getJSON("get_job.php",function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.id+"'>"+item.job_name+"</option>";
});
$("#job_id").html(items);
});
});
</script>
PHP file get_emp.php
<?php
$mysqli = new mysqli($mysql_hostname,$mysql_user, $mysql_password, $mysql_database);
$q = "select id, fullName from employees";
$sql = $mysqli->query($q);
$data = array();
while($row = mysqli_fetch_array($sql, true)){
$data[] = $row;
};
echo json_encode($data);
?>
PHP file get_job.php
<?php
$mysqli = new mysqli($mysql_hostname,$mysql_user, $mysql_password, $mysql_database);
$q = "select id, job_name from jobs";
$sql = $mysqli->query($q);
$data = array();
while($row = mysqli_fetch_array($sql, true)){
$data[] = $row;
};
echo json_encode($data);
?>
One more time, I appreciate your time taking a look at this code trying to give me a hand.
Thank you.
Code looks okay to me. Have you set the correct header?
header('Content-Type: application/json');
echo json_encode($data);
At first glimpse the code looks all right. What did you have in the console? Is all the json data there? Otherwise try
$.each(data,function(index,item)
{
$('<option>').val(item.id).html(item.job_name).appendTo("#job_id");
});
Updates:
can you please try adding
error_log(print_r($data,1));
before
echo json_encode($data);
in get_emp.php and check the php_error.log to see if the data is populated on the server side when you load the page
I think it has todo with timing between DOM ready and executing the Jquery script.
In this case your script executes before the DOM is ready. If some objects you are refering to, arent ready yet in the DOM, thus JQuery cant find the object, then JQuery simply stops executing.
I'm having a very annoying issue and I have no idea what I'm doing wrong. It has (again) something to do with loading in a page into a DIV.
I have made a form which can be used to update information into a database. This form and PHP code is in one file and is being loaded in one DIV. When I visit the page which is loaded into the DIV itself, everything is working fine and the database is being updated as it should.
Though, when the page is loaded into the page. When I press submit nothing happens. What I want to happen is that the echo, which is either "Success!" or "Error!" is being displayed in the same DIV as the page is loaded into. This is my code, I hope someone can help! some variables are in Dutch, excuse me for that.
if(isset($_POST['submit'])) {
include "database.php";
session_start();
$id = $_POST['id'];
$titel = $_POST['titel'];
$text = $_POST['text'];
$categorie = $_POST['categorie'];
$auteur = $_SESSION['sess_loginnaam'];
$laatst_aangepast = date("Y-m-d H:i:s");
$sql="UPDATE paginas SET id='$id', titel='$titel', text='$text', categorie='$categorie', auteur='$auteur', laatst_aangepast='$laatst_aangepast' WHERE id='$id'";
$result=mysql_query($sql);
if($result){
echo "Success!";
?>
<META HTTP-EQUIV="refresh" content="2;URL=index.php">
<?php }
else {
echo "Mislukt!";
}
}
else {
include "database.php";
$id = $_GET['id'];
$sql="SELECT * FROM paginas WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
<form name="form1" method="post" action="">
ID:
<input name="id" type="text" id="id" value="<? echo $rows['ID']; ?>" size="2"></div> Titel:
<input name="titel" type="text" id="titel" value="<? echo $rows['titel']; ?>" size="50%"> Categorie: <select name="categorie"><option value="Paginas">Pagina's</option>
</select>
Tekst:
<textarea name="text" type="text" id="text" rows="31" cols="79"><? echo $rows['text']; ? ></textarea>
<button type="submit" name="submit">Edit!</button>
</form>
And here is the code I use to load this page into the DIV:
$("#edit").on('click',function(){
$('#content').load($(this).attr('href'));
});
So how can I manage to display the echo into the same DIV? :)
$.load is equivalent to $.get whereas your php code detect $_POST, that's way there is no response.
You can change your js code to
$("#edit").on('click',function(){
$.post($(this).attr('href'), YOUR_FORM_DATA_HERE, function(data){
//update your page here with response
});
});
See $.load - jQuery Doc