Show MySQL Results in a table without reloading page [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to write a web application for database managing purposes. I've written front-end of the app on HTML+CSS3 and the server-side language is PHP, but I'm still confused about how to make MySQL calls and show it to the user without refreshing/reloading the page.
Trying to be specific, here are my doubts:
Can I execute a MySQL query without reloading a page?
How do I show a table with query's results?

Use Ajax for this :
Add this code to main page where you want to display table data
<html>
<head>
<script>
function dashboard() {
var query_parameter = document.getElementById("name").value;
var dataString = 'parameter=' + query_parameter;
// AJAX code to execute query and get back to same page with table content without reloading the page.
$.ajax({
type: "POST",
url: "execute_query.php",
data: dataString,
cache: false,
success: function(html) {
// alert(dataString);
document.getElementById("table_content").innerHTML=html;
}
});
return false;
}
</script>
</head>
<body>
<div id="table_content"></div>
</body>
</html>
In table_content div the data come from execute_query.php page will load without refreshing the page.
execute_query.php
$user_name = $_POST['parameter'];
$query="SELECT * from info where name=$user_name";
$result=mysql_query($query);
$rs = mysql_fetch_array($result);
do
{
?>
<table>
<tr>
<td><?php echo $rs['city']; ?></td>
<td><?php echo $rs['phone_number']; ?></td>
</tr>
</table>
<?php
}while($rs = mysql_fetch_array($result));

Related

Display that User Current Location as the value of the Textbox [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have here a codes that displays the current location of the user, this codes is already working. My problem is how to put the id "#location" as the value for my textbox in html form so that the current location will put in a textbox form.
$(document).ready(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showLocation);
} else {
$('#location').html('Geolocation is not supported by this browser.');
}
});
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.ajax({
type:'POST',
url:'../geo/getLocation.php',
data:'latitude='+latitude+'&longitude='+longitude,
success:function(msg){
if(msg){
$("#location").html(msg);
}else{
$("#location").html('Not Available');
}
}
});
}
HTML:
Location: <input id="location" type="text" value="" size="50">
<br>
If the ajax request works correctly (we don't know what kind of data it returns), when the problem is in wrong choice of method.
Use .val() instead of .html() because you can't insert html inside input.
if (msg) {
$("#location").val(msg);
} else {
$("#location").val('Not Available');
}

Send variable From PHP to AJAX [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a php page called posts.php, this page just select all from a database table named posts and display them. Down the page I have a textarea named post. When I press the submit button the AJAX script will send the text from textarea to a page called insert.php. This insert.php gets the submited text and insert it into posts table. How can I do this and how can I upload the posts.php when I have inserted a post into posts table.
Example:
User 123 writes a message in the textarea. He presses submit button. AJAX sends the post to insert.php. Insert.php inserts the post into posts tabel. Posts.php shows the message.
You can do it very easily! Here's a very simple example.
func.js
$(document).on("click", ".submitBtn", function() {
var textareaVal = $(".textarea").val();
$.ajax({
type: "POST",
url: "insert.php",
dataType: "text",
data: { postVar: textareaVal },
success: function(data) {
$(".resultDiv").load("posts.php");
}
});
});
insert.php
// You have to pay attention to AJAX can only invite your file!
if (!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") {
if (isset($_POST["postVar"])) {
$var = sanitize($_POST["postVar"]); // You need sanitize the data!
// Insert into your table...
}
}
In your posts.php, SELECT datas from your table, but only select the data, without „HTML container”! Your JS file load this datas on your resultDiv box.
I'm not certain why you would need to do this via ajax, but:
$( "#myForm" ).submit(function( event ) {
$.ajax({
type: 'post',
url: '../insert.php' ,
data: { value1: document.getElementById('textarea').value },
success: function () {
alert("yay!");
}
});
});

How can i know the page is runing in browser or not? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
how can I know the page is really opened in brower or get from script or src="xxx"?
<?php
session_start();
if(!isset($_SESSION['count'])){
$_SESSION['count']=0;
}
// I don't want to
// if(open in brower){
$_SESSION['count']++;
// }
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
url:'index.php',
type:'GET',
data:{},
success:function(){}
});
</script>
<?php
echo $_SESSION['count'] ; // the result now is 1,3,5,... for every reload
// I want to get the result 1,2,3,4... for every reload , how to check the page is really open in brower? not run in ajax or get ?
?>
A simple solution to this specific example would be to pass some data to the ajax request, and check that the data ISN'T present in order to increase your session count.
<?php
session_start();
if(!isset($_SESSION['count'])){
$_SESSION['count']=0;
}
if(!isset($_GET['dontcount'])){
$_SESSION['count']++;
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
url:'index.php',
type:'GET',
data:{'dontcount',1},
success:function(){}
});
</script>
<?php
echo $_SESSION['count'];
?>

need advice and bug help please! (HTML and JS, maybe PHP and MySQL) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make an auto complete forum (like the tags below) that works in lime-survey. I'm new to this so please explain everything like i'm 5 :)
My goals are:
auto complete to work
work with lime-survey
have an excel file or maybe a database using PHP/MySQL that anyone can manage by editing. It would only be ONE row. Please point me how to do this. I use a mac :)
Here is the code:
<!DOCTYPE HTML>
<html>
<body>
<div >
<input type="text" id="raw_input" width="100" height="30" name="box" onkeyup=show(this)>
</div>
<div id="drop_down" style="display:none">
<SELECT id=box size=3 name="box" onclick=show(this)></SELECT>
</div>
<script>
function drop_the_box() {
document.getElementById("drop_down").style.display = "none";
document.getElementById('box').length = 0;
if (!randomcharactersblablabla).test(document.getElementById("raw_input").value){
document.getElementById("drop_down").style.display="block";
var database = new Array("object_1","item_1","object_2","item_2");
var string = document.getElementById("raw_input").value;
for (var s = 0; s < database.length; s+= 1 ) {
var t += 1
if (database[s].indexOf(string) != 0) {
addItem(string[s],database[s]);
scan(streetArray[s],streetArray[s]);
}
}
}
}
function scan(x,y) {
var ghost_tag = document.createElement("ghost");
document.getElementById("box").options.add(ghost_tag);
ghost_tag.text = x;
ghost_tag.value = y;
}
function show(visable) {
document.getElementById("dropdown").value = visable.value;
}
</script>
</body>
</html>
Keep you data in mysql database. Create php file which will handle queries. Use jquery.ajax() to send queries and retrieve responses from php file.
Use this example
jQuery file
$('#search').change(function(){
var name = $('#search').val();
$.ajax({
type: 'POST',
url: 'request.php',
data: 'some data(may be variable)',
success: function(response){
$('#searach').val(response);
}
});
})
php file
if(isset($_POST['some_data'])){
$query = 'SELECT your_table_field FROM your_table WHERE your_table_field LIKE %$_POST['some_data']% LIMIT 1';
$result = mysql_query($query);
$myrow = mysql_fetch_array($result);
echo $myrow[0];
}

How to reference an ID to delete [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I´m trying to remove a div with jquery but I don´t know how can I refer to the ID because it´s a PHP var: id="delete$count"
My Javascript code:
<script type="text/javascript">
function deleteFile(fname,directory)
{
$.ajax({ url: "delete_img.php",
data: {"file":fname,"directory":directory},
type: 'post',
success: function(output) {
alert(output);
$('div').remove();
}
});
}
</script>
My PHP code:
$count=1;
echo '<div class="gallery-item" id="delete$count"></div>';
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\")'>
</a></div>";
$count++;
I think the below will send delete$count verbatim to the response stream without evaluating $count
echo '<div class="gallery-item" id="delete$count"></div>';
Change it to
echo "<div class='gallery-item' id='delete$count'></div>";
so as for PHP to interpolate $count.
Try then passing this as a third parameter to your function as
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\",\"delete$count\")'>
and collect its value in a third argument to
function deleteFile(fname,directory, deletecount) { }
and replace
$('div').remove();
by $('#' + deletecount).remove();
If i understund correctly, you want to achieve the following:
use AJAX to tell server to delete file
remove same file from current view without reloading page
If you read file information from database, i would suggest you to use row-id or something similar as 'gallery-item' ids, and as argument for deleteFile() function.
In case you dont, you can use paired value of $directory and $file instead.
echo '<div class="gallery-item" id="'. $directory . $file . '" ></div>';
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\")'>
</a></div>";
With this, you can replace your $('div').remove() with$('#'+directory+fname).remove()

Categories