I am trying to conctenate/embed database value from php script into my api URL
but i don't know how to get it right, I am not familiar with php
it needs to embed the varible from DB in order to access API values like this
api/rfq/1 (example)
I tried this so far
php code:
<?php
require_once('connection.php');
session_start();
if(!$con){
die('Please Check Your Connection'.mysqli_error());
}else{
$query="select BusinessID from business where username='".$_SESSION['User']."'";
$result=mysqli_query($con,$query);
while($row =mysqli_fetch_array($result)){
$results[]=implode($row['BusinessID']);
}
echo json_encode($results);
// echo 'Works ASF '+$hey;
}
?>
jQuery file:
$(function() {
$.getJSON('getID.php', function(data) {
$.each(data, function() {
alert(data.column);
})
})
var $hello = $('#hello');
$.ajax({
type: 'GET',
dataType: "json",
url: 'http://slimapp/api/rfq/"'+$_SESSION['User']+'"',
success: function(hello){
$.each(hello, function(i,order){
$hello.append('<div class="panel panel-default"><div class="panel-thumbnail"></div><div class="panel-body"><p class="lead"><b><img src="assets/img/Profile.png" height="28px" width="28px">'+order.Title+'<i class="glyphicon glyphicon-plus"></i>Make Bid</b></p><p><img src="assets/img/RFQ.png" height="28px" width="28px">'+order.Description+'</p><p><img src="assets/img/Clock.png" height="28px" width="28px">'+order.Date+'</p><hr/><p><img src="assets/img/email.png" height="28px" width="28px">'+order.email+'</p><p><img src="assets/img/Phone.png" height="28px" width="28px">'+order.cellphone+'<p><textarea class="form-control" placeholder="Comment"></textarea><br/><li id="btn1"></li><button class="btn btn-success pull-left" type="button" style="border-radius:12px">Comment</button></p></div></div>');
})
}
})
});
I expect to get data from url like this http://slimapp/api/rfq/1
You Jquery file should look like this:
function apiCall(user) {
var url = 'http://slimapp/api/rfq/'+ user +''; // This var is creating the right URL, using the parameter USER of the function
$.ajax({
type: 'GET',
dataType: "json",
url: url, // Calling the new right URL
success: function(hello){
// Handle your success
}
})
};
Here you are creating a new function, that receives the parameter User that you wanna pass in the URL.
Your PHP file should look like this:
<?php
$results = '1';
?>
<script>
apiCall(<?php echo $results ?>); // Passing the value 1 as a parameter for the function
</script>
Here you are calling the function apiCall and passing the value of the variable $results as parameter.
Related
I'm trying to dynamically change a modal's content with the result of a query using PHP.
So far, I've managed to reach the ajax call, but I'm not getting a success nor error message
PHP: get_user.php
<?php
include ("session-connection.php");
session_start();
$result = $_POST['param'];
echo $result;
?>
HTML
<script type="text/javascript">
function view_progress(row){
console.log("id: " + row);
var param = row;
$.ajax({
data: param,
url: 'get_user.php',
type: 'post',
beforeSend: function(){
$("#progressModal").modal('show');
$("#alert_name").html("...");
},
error: function (){
$("#alert_name").html("Error");
},
success: function(resultado){
$("#alert_name").html(resultado);
}
});
}
</script>
If the code is successfull it should change the label alert_name with the id the javascript function recieves.
I'm trying to display the search result of my page under the the search area. So I used AJAX to display the result in a div. but I could'nt get it work.
I have three main pieces, the div, the searchResult page and the ajax function
<input type="text" name="studentName">
<button type="submit" name="searchByName" onclick='get_info();'>بحث</button>
<div id="searchResult"><b></b></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function get_info() { // Call to ajax function
$.ajax({
type: "POST",
url: "NameSearchResult.php", // Name of the php files
data: {name: <?php echo $_POST['studentName']; ?>},
success: function(html)
{
$("#searchResult").html(html);
}
});
}
and my search Page:
<?php
include_once 'dbConfigBDO.php';
$studentName = $_POST["name"];
$counter=0;
$emptyString = "لايوجد";
$sql = "SELECT * FROM Student";
$result = $conn->query($sql);
$row_count = $result->rowCount();
if ($row_count > 0){
.......... }
Now when I search nothing appears, although it works when I put all the code in one page (which would be messy in term of the appearance of the result!).
From function return the output as per below:
return json_encode($result);
In ajax call use dataType:"json" and show your html
Example ajax call:
$.ajax({
type: "POST",
dataType:"json",
url: "NameSearchResult.php", // Name of the php files
data: {name: $("#studentName").val()},
success: function(html)
change code like this
<input type="text" name="studentName" id="studentName">
<button type="submit" name="searchByName" onclick='get_info();'>بحث</button>
<div id="searchResult"><b></b></div>
<script>
$.ajax({
type: "POST",
url: "NameSearchResult.php", // Name of the php files
data: {name: $("#studentName").val()},
success: function(html)
{
$("#searchResult").html(html);
}
});
}
</script>
inside ajax success method try catch what you are getting
success: function(html)
{
console.log(html);
}
if you getting something then your code must be work.
I've a link in my php page. Which I want to send a id request to server. But it doesn't response correctly. Actually, I've got two pages.
a. users.php
b. user_update.php
So I'm trying a send a request to the server using jquery/ajax by user id from users.php page to user_update.php for update the details and get back the successful result to user.php page.
The code is look like this:
<div id="edit_user_details">Edit Details</div>
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>
$.ajax({
url: edit_user_details.php,
type: post,
data: id;
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
But it's not working properly. is there anything I'm missing because of I'm new learning of Jquery/Ajax. :)
Update:
I'm requesting this id in php while loop. It's look like this..
while($search_result = mysql_fetch_array($getUser)){
$uid = (int) $search_result['user_id'];
<div id="edit_user_details">Edit Details</div>
}
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>;
$.ajax({
url: 'edit_user_details.php',
type: 'post',
data: {'id' : id},
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
Is it wrong ? What I'm trying is... I want to update user details base on their id. So what need to do for this ?
you need to create a unique id in loop or create a function and calll it onclick like
<?php while($search_result = mysql_fetch_array($getUser)){
$uid = (int) $search_result['user_id']; ?>
<div id="edit_user_details">Edit Details</div>
<?php }?>
<script>
function myfunc(id) {
id = id;
$.ajax({
url: 'db_search.php',
type: 'post',
data: {'id' : id},
success: function(response) {
$('#edit_user_result').html(response);
}
});
}
</script>
you should end your id initialization with a semi-colon;
id = <?php echo $uid; ?>;
Check the correct way of using ajax in this example: Demo ajax using JQuery
Make changes as shown below and try again..
1: Add 'post' in type.
2: Add ''edit_user_details.php' in url
3: Add semicolon at the end here
<?php echo $uid; ?>;
4: take your id as shown in below code
Updated code :-
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>;
$.ajax({
url: 'edit_user_details.php',
type: 'post',
data: {'id' : id},
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
For more information :-
https://api.jquery.com/jQuery.ajax/#entry-examples
i think you are not declaring var id before
try
<div id="edit_user_details">Edit Details</div>
<script>
$('#edit_user_details').click(function() {
var id = <?php echo $uid; ?>;
$.ajax({
url: edit_user_details.php,
type: post,
data: [id:id],
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
Check your url inside $.ajax. If you are sending your data to user_update.php then your code should be as follows :
<script>
$('#edit_user_details').click(function() {
id = <?php echo $uid; ?>
$.ajax({
url: user_update.php,
type: post,
data: id,
success: function(response) {
$('#edit_user_result').html(response);
}
});
});
</script>
Just change your url to user_update.php and put a comma(,) after data:id in the place of semicolon(;).
This code will works fine for what you need and with less lines, assuming that the PHP var $uid will be processed on loading the page:
<div id="edit_user_details">Edit Details</div>
<script>
$('#edit_user_details').click(function() {
$.get('edit_user_details.php', 'id=<?php echo $uid; ?>', function(response){
$('#edit_user_result').html(response);
});
});
</script>
Ok, so I have this search box in which people typein a food item. When they press the button I need that input to be send to a .php file. That php file will look op the calories of that food item (thats in my database) and output the food item name and calories. All this need to be done without reloading the page so I started figuring out how JQuery works.
However I am stuck, I don't know what to put in the data field of the jquery function and how I can 'catch' that data in the .php file. Can someone give me an idea? thanks a lot! (see the ??????'s for things i don't understand). Also, the data that comes back needs not to be in an alert box in the end, but update some table on my page, how can i do this? which JSON (?) Jquery function do I need?
what I have up until now:
in head:
<script type="text/javascript">
function contentDisp()
{
$.ajax({
type: 'POST',
url: 'getFood.php',
data: '????????',
success: function(data){
alert("Data Loaded: " + data);
}
});
}
</script>
and in body:
<form autocomplete="off">
<p>
Product:
<input type="text" name="food" id="food" class="food_name_textbox" onmouseover="javascript: this.className='food_name_textbox_mouseover';" onmouseout="javascript: this.className='food_name_textbox';" / >
</p>
<button id="zoek" type="button" onClick="contentDisp();">Zoek</button>
</form>
and in getFood.php:
<?php
require_once "config.php";
$id = "??????"
$result = mysql_query("SELECT * FROM voedingswaarden WHERE voedsel='$id'");
$row = mysql_fetch_array($result);
echo json_encode($row);
?>
$.ajax({
type: 'POST',
url: 'getFood.php',
data: {'foodnametextbox' : $('#food').val() },
datatype: "json",
success: function(data){
$('#table').html(data);
}
});
<?php
require_once "config.php";
$id = $_POST['foodnametextbox']; //escape and validate this input before using it in a query
$result = mysql_query("SELECT * FROM voedingswaarden WHERE voedsel='$id'");
$row = mysql_fetch_array($result);
echo json_encode($row);
?>
<script type="text/javascript">
function contentDisp()
{
var textSearch = $("#myText").text();
$.ajax({
type: 'POST',
url: 'getFood.php',
data: textSearch,
success: function(data){
alert("Data Loaded: " + data);
}
});
}
</script>
PHP:
$id = $_POST['textSearch'];
i have a form in which if the user enters the search query, its parameter should be passed through jquery and after getting the results it should load the results in the div container. since i'm not very well-versed with jquery, how would i do this?
html:
//currently the data is being displayed on pageload:
$(document).ready(function() {
$("#bquote").load("quotes_in.php")
});
$(".bsearch")
.keydown(function() {
//pass the parameter to the php page
//display in div #bquote
});
<!-- Begin Search -->
<div id="search" style="display:none;">
<input type="text" class="bsearch" name="search" value="Search" />
</div>
<!-- End Search -->
php [using OOP]:
if (isset($_POST["search"]))
{
$quotes->searchQuotes();
}
php class:
$search = $_POST['search'];
$sql = "SELECT * FROM thquotes WHERE cQuotes LIKE '%"
. mysql_real_escape_string($search) ."%' ORDER BY idQuotes DESC";
try {
$query = $this->_db->prepare($sql);
$query->execute();
if(!$query->rowCount()==0)
{
while($row = $query->fetch())
{
echo $this->formatSearch($row);
}
I would do in that way:
$(".bsearch").keydown(function() {
//create post data
var postData = {
"bsearch" : $(this).val(),
"anotherParam" : "anotherValue"
};
//make the call
$.ajax({
type: "POST",
url: "yourAjaxUrl.php",
data: postData, //send it along with your call
success: function(response){
alert(response); //see what comes out
//fill your div with the response
$("#bquote").html(response);
}
});
});
EDIT:
For putting a loader you need to check this here:
http://api.jquery.com/category/ajax/global-ajax-event-handlers/
And to show a loader image for example:
$("#loading").ajaxStart(function(){
$(this).show();
});
And to hide it when ajax call is complete:
$("#loading").ajaxComplete(function(){
$(this).hide();
});
If you want to go down the ajax route...
$(".bsearch").keydown(function() {
// Get the current input value
var value = $(this).val();
//pass the parameter to the php page
$.ajax({
type: "POST",
url: "some.php", // replace this with the right URL
data: "bsearch=" + value,
success: function(msg){
$("#search").html(msg);
}
});
});
Read up on jQuery.ajax() and if you turn that search box into a proper form, utilize jQuery.serialize()
Instead of using $_POST, you can use $_GET or $_REQUEST like the following:
var searchString = $(".bsearch").val();
$("#bquote").load("path_to_php_file.php?search="+searchString);
Then, in PHP, replace
$_POST
...with
$_GET