I have a form my conditions are,
form.php, where I am inserting data using form,
display.php where I am showing data in table form using pagination and
validation.js where I am validating form data as well as I have following function
$('#pagiCount a').click(function(){
$.get($(this).attr('href'), function(response) {
console.log(response);
$( "#result" ).html(response);
});
return false;
//alert($(this).attr('href'));
});
Now I want to show that form from form.php on same display.php after pagination table.
How I can do that ?
New Suggeston :
Whenever you display the data in the ajax.php , just create a session array variable and update that value in the session array variable. So when ever you display the value in the ajax.php , get the data from the session array variable and display it.
Code : ajax.php
while($result=mysql_fetch_array($query)){
$_SESSION['compare_array'][]=$result;
}
echo "<table>";
foreach($_SESSION['compare_array'] as $key => $result)
{
echo '<tr><td>'.$result['brand'].'</td>'.'<td width="150" height="10">'.$result['model'].'</td>'; echo '<td>'.$result['clr'].'</td>'.'<td>'.$result['sim'].'</td>'.'<td>'.$result['os'].'</td>'.'</td></tr>';
}
echo "</table>";
OLD Suggestion :
You can use $.ajax with datatype : json. so you can return the data array in json encoded format so you can return both pagination result data and the form html data in array.
echo json_encode("pagination_response" => "put pagination html here", "form_data_response" => "put form html here ");
$('#pagiCount a').click(function(){
$.get($(this).attr('href'), function(response) {
console.log(response);
$( "#result" ).html(response);
});
return false;
//alert($(this).attr('href'));
});
$.ajax({
type: "POST", // post or get
url: 'your_url',
data: "data_href="+ $(this).attr('href'),
dataType: "json",
success: function(response)
{
if(result.status =="success")
{
$( "#result" ).html(response.pagination_response);
$( "#result_form_html" ).html(response.form_data_response);
}
});
Related
I use web services, i try this code. i want to select and show my php page , select 1.string or 2.string
ajax.php
$bb=json_decode($result,true);
$a= strlen($bb[$user_id]['1']);
if ($a!=0)
{
$this->data[$bb];
echo "<br>User ID===> ".$bb[$user_id]['1'];
echo "<br>name===> ".$bb[$user_id]['2'];
echo "<br>surname===> ".$bb[$user_id]['3'];
echo "<br>etc..===> ".$bb[$user_id]['4'];
}
x.php Js codes
$.ajax({
type:'post',
data:{user:user,user_id:user_id},
url:'<?php echo site_url('ajax/hello');?>',
success: function(result){
$('#htmlname').html(result);
}
});
});
});
Return the array as JSON. To do this, set the dataType to JSON in your ajax call: dataType: "json"
You can then access each element of the array in the success callback:
success: function (result) {
var item1 = result[0];
var item3 = result[2];
}
I am trying to send information using AJAX from JavaScript in one file to PHP in another file. When I inspect the page on chrome after I have pressed the button, it shows the values under the form data sub section in networks, however, if I var_dump[$_POST] it shows it is empty.
Here is my JavaScript:
function details(id) {
var data = {"id" : id};
jQuery.ajax({
url: "/Example/includes/detail.php",
type: 'POST',
data: data,
success: function(data){
jQuery('body').append(data);
},
error: function(){
alert("Error");
}
});
}
Here is the PHP:
<?php
$id = $_POST['id'];
$id = (int)$id;
?>
Any help would be greatly appreciated. Thanks
I cant find answer to my problem...the truth is im new to jQuery and JSON.
On login page i want to display top customers and top tracks, when page loads.
Iv displyed them via echo from php, but i want to create JSON object and send it to login ...loop there and display it in unordered list.
Can you guys help me with creating JSON and displaying it in jquery.
Here's my code:
jQuery:
/* Function to load top Customers */
function loadCustomers() {
/* Create data string to call functions in php*/
var dataString ="function1=loadCustomers&function2=loadTracks";
$.ajax({
type:"GET",
url: "login.php",
data: dataString,
dataType: "json",
success: function(data) {
$("#error").show();
$("#errormsg").html(data.FirstName);
//how can i display Json data in unordered list ? #customerList
},
error: function() {
$("#error").show();
$("#errormsg").text("cant display data");
}
});
}
PHP :
}else if ($_SERVER["REQUEST_METHOD"] == "GET") {
//JSON customers array
$customers = array();
if ($_GET["function1"] == "loadCustomers") {
try {
$customerStmt = $conn->prepare("Select customer.FirstName, customer.LastName, customer.City, sum(invoice.Total) from invoice INNER JOIN customer on invoice.CustomerId = customer.CustomerId group by invoice.CustomerId order by sum(invoice.total) DESC LIMIT 5");
$customerStmt->execute();
$customerRows = $customerStmt->fetchAll();
//how to create JSON data to send it
header("Content-type: application/json");
echo json_encode($customerRows);
}catch (PDOException $e) {
$e->getMessage();
}
}
Well, from the code it looks like you are responding with multiple customers, but in jQuery you are accessing the data directly without accessing the array. So it should look like this
$.ajax({
type:"GET",
url: "login.php",
data: dataString,
dataType: "json",
success: function(data) {
var items = [];
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "#customerList" );
},
error: function() {
$("#error").show();
$("#errormsg").text("cant display data");
}
});
The problem was i couldn't recive JSON data from PHP.
After hours of googling and trying diffrent solutions i finally found an error..
When you read from database you have to set it to utf-8 charset.
I was fixing this problem in code with utf8_encode function...
You have to set it in new definition of PDO object :
$conn= new PDO("mysql:host=$server;dbname=$database;charset=utf8", $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
After this change im sending JSON data .
I'm trying post data to PHP file but i can't receive any data from PHP file. Let me add codes.
This is my jQuery function:
$(document).ready(function () {
$(function () {
$('a[class="some-class"]').click(function(){
var somedata = $(this).attr("id");
$.ajax({
url: "foo.php",
type: "POST",
data: "id=" + somedata,
success: function(){
$("#someid").html();
},
error:function(){
alert("AJAX request was a failure");
}
});
});
});
});
This is my PHP file:
<?php
$data = $_POST['id'];
$con = mysqli_connect('localhost','root','','somedatabase');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"database");
$sql="SELECT * FROM sometable WHERE id = '".$data."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo $row['info'];
}
mysqli_close($con);
?>
This what i have in HTML file:
<p id="someid"></p>
Data1
Data2
Note: This website is horizontal scrolling and shouldn't be refreshed. When i'm clicking links (like Data1) it's going to another page without getting data from PHP file
You have a few problems:
You are not using the data as mentioned in the other answers:success: function(data){
$("#someid").html(data);
},
You are not cancelling the default click action so your link will be followed:$('a[class="some-class"]').click(function(e){
e.preventDefault();
...;
As the id's are integers, you can use data: "id=" + somedata, although sending an object is safer in case somedata contains characters that need to be escaped:data: {"id": somedata},;
You have an sql injection problem. You should cast the variable to an integer or use a prepared statement:$data = (int) $_POST['id'];;
As also mentioned in another answer, you have two $(document).ready() functions, one wrapping the other. You only need one.
success: function(){
$("#someid").html();
},
should be:
success: function(data){
$("#someid").html(data);
},
You should add parameter in success
success: function(data){ //Added data parameter
console.log(data);
$("#someid").html(data);
},
The data get the values what you echo in PHP end.
This:
success: function(data){
$("#someid").html(data);
},
and you have two document ready, so get rid of:
$(document).ready(function () { ...
});
data: "id=" + somedata,
Change it to:
data: { id : somedata }
I am trying to get the id, where the user clicked, and pass it to my controller via AJAX.
For some reason, when i try to show what i am receiving in my controller, it show an empty array.
Any idea why my alert return an empty Array?
JS Function:
function categorySelected(elem) {
$.ajax({
url: "/newgallery/creative-fields-click",
type: "POST",
data: elem.id
}).done(function (response) {
alert(response);
});
}
PHP:
public function creativeFieldsClickAction()
{
$idSelected = $this->_request->getPost();
print_r( $idSelected );
exit();
}
Data should be JSON, not integer.
data: { elemid : elem.id }
Then in PHP data can be found from $_POST['elemid']. In your code it could be like
$this->_request->getPost()['elemid'];
Or something like that.
Try by changing
In ajax
data: { id : elem.id }
And in action of controller
$ids = $_POST;
print_r($ids);