Retrieving data from PHP multidimensional array with JQUERY - php

I have an array that looks like that in a PHP page:
$page["1"] = array("element1","element2","element3","element4");
$page["2"] = array("element1","element2","element3","element4");
$page["3"] = array("element1","element2","element3","element4");
I need to retrieve some data from this array in a JAVASCRIPT file (JQUERY).
How can I import for example the elements4 $page[1][3],$page[2][3],$page[3][3] ?
I have seen many examples here with JSON but not like this one...

May be this can help you(Filename: getdata.php):
<?
if($_GET['ajax']==1){
$page = array(
array("element1","element2","element3","element4"),
array("element1","element2","element3","element4"),
array("element1","element2","element3","element4"),
);
echo json_encode($page);
}else{
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button class="getdata" >Get Data</button><div class="jsdata"></div>
</body>
<script>
$('.getdata').click(function(){
$.ajax({
type:"POST",
url:"getdata.php?ajax=1",
success:function(data){
var res = eval("("+data+")");
$('.jsdata').html("$page[1][3],$page[2][3],$page[3][3] respectively:"+res[0][3]+","+res[1][3]+","+res[2][3]);
}
})
});
</script>
</html>
<? } ?>

Related

Create php with a jQuery and php include

Well, I've created a code to include a PHP page in a box and not only the normal include ('');
This is my code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
$(function() {
var count = 1;
$('#append').click(function() {
if (count < 2) {
$('#parent').append('<div id="first' + count + '">text</div>');
$('#first1').load('../image/index.php');
count++;
}
});
});
</script>
<a id="append">Add DIV 1</a>
<div id="parent">
</div>
</body>
</html>
Now, I've noticed I could "load" a page in html, but all the php and javascript code is "out".
How do I do to have the "real" page inside another.
The PHP code is not returned since it is executed on the server before you get the HTML back from it.
In order to just retrieve it as plain text you will need to change the file you are trying to retrieve.
As far as how to load a page inside another page, you can use iframes for that.
Try this:
<script>
$(function(){
var count = 1;
$('#append').click(function(){
if (count <2){
$('#parent').append('<div id="first'+count+'">text</div>');
$.get('http://test.com/image/index.php',function(data){
$('#first1').html(data);
});
count++;
}
});
});
</script>

Trying to Send data using jquery .post()

In insert.php I have the following
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".like").click(function() {
var data = $(this).attr('data-id');
$.post("data.php", {'name': data}, function(response){
$('#dv').html(response);
});
});
});
// ...
</script>
<!-- ... -->
<?
if (!is_bool($result) && !is_null($result)){
while($row = mysql_fetch_array($result))
{
?>
<? $id = $row['uniqueid'];?>
<? echo "<br>ID: " . $row['uniqueid'];?>
<? echo "<br>Name: " . $row['surname']; ?>
<? echo "<br><a href class=like id=buton data-id='$id'> Like (" . $row['likes'] . ") </a>"; ?>
<? echo "<br><br>"; ?>
<!-- ... -->
in data.php I have
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
alert("<? echo $_POST['name']; ?>");
</script>
I fixed a couple of things I was told were wrong but it still does't work. The alert box is blank. Please help. Thanks.
this is completely wrong... for your data.php this will not parse javascript the way you are expecting.. it will simply take in POST data and ECHO out a result.. dont try and use client-side javascript because it will not do anything
data.php: its php just simply do
<?php
echo $_POST['name'];
?>
in your js if you really want a popup to show then you have to wait for the results of the POST/RESPONSE and do it
$.post("data.php", {'name': data}, function(response){
$('#dv').html(response);
alert(response);
});
remember the php script you call from a POST or ajax call will only process server side code.. PHP, perl, java, C... not client-side code like javascript

php echo not working for strtotime function while assigning it to a javascript variable

This works fine
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var timeLeft = <?php echo '1279'; ?>;
$("#time").text(timeLeft);
});
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>
but this doesn't
<script>
$(document).ready(function(){
var timeLeft = <?php echo strtotime("now"); ?>;
$("#time").text(timeLeft);
});
</script>
It gives error Uncaught SyntaxError: Unexpected token < .
It is a php extension file.
Try this:
<script>
$(document).ready(function(){
var timeLeft = '<?php echo strtotime("now"); ?>';
$("#time").text(timeLeft);
});
</script>
Directly using php in javascript is very dangeorous. Because timetostr may output some warnings about timezone etc and your code will be broken without any visual clue.
In my projects, If I have to use any php output in my javascript, I create hidden divs and access the value from javascript:
<div class='hidden' id='values'>
<div class='val-time'><?php echo strtotime("now"); ?></div>
</div>
<script>
$(document).ready(function(){
var timeLeft = $("#values .val-time").html();
$("#time").text(timeLeft);
});
</script>
So whenever you want to debug your code, simply remove "hidden" class from #values and look at which values are ouputted by PHP.

Can't use PHP value from jQuery.load() content

I'm working on a project that uses jQuery modals that dynamically load their content on a button click.
I am having an issue using this loaded content like I would normally.
Here is an example of my problem
<script type="text/javascript>
click function{
load modal{
open: $('#modalID').load('phpfile.php?id=<?php echo $id ?>');
}
</script>
That all works fine, but when trying to use jQuery within the "phpfile.php" is where the problem lies
<?php
$id = $_GET['id'];
$db = USE ID TO GET DATABASE INFO; //works fine here
?>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
When I click the button, I get the alert but it just says test and I don't get the ID like I should.
Thanks in advance for your help and suggestions on this one!
You have pasted pseudo code so it is difficult to say what is wrong in your code.
The following should work, give it a try:
File 1.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?
$id = "10";
?>
<script>
$(document).ready(
function(){
$('#modalID').load('phpfile.php?id=<?php echo $id ?>');
});
</script>
<div id = 'modalID'></div>
And phpfile.php:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?php
$id = $_GET['id'];
?>
<script>
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
<input type="button" id = "buttonID" value="click me">
Please check on the do you have any errors in your browser's console. The above code should work. The possibilities are like if you don't get your $_GET['id'] or any script errors will prevent execution of your code.
I have created a sample php file which is working fine.
<?php
$id = rand();
?>
<button id="buttonID"> Click </button>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>

Inserting json data into html page in list

I'm trying to get information from mysql and post the information into an html page. Here's what I've got so far:This is my tenantlistmob.php
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM tenanttemp");
while ($row = mysql_fetch_assoc($result))
{
$array[] = array($row['TenantFirstName']);
}
echo json_encode($array);
?>
When i call tenantlistmob from browser directly it shows [["Humayun"],["Sahjahan"],["Bayezid"],["Bayezid"],["Asaduzzaman"],["Mouri"]] where firstnames are comming. I like to use this name in html page. my html page is
<!DOCTYPE HTML>
<html>
<link rel="stylesheet" href="styles/main.css" />
<script type="text/javascript" src="jquery.js"></script>
<body>
<div id="output">this element will be accessed by jquery and this text replaced</div>
<script id="source" type="text/javascript">
$(function ()
{
$.ajax({
url: 'tenantlistmob.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data;
//var vname = data[1]; //get name
$.each(id, function (val)
{
$('#output').html(""+id);
});
}
});
});
</script>
<form id="formset">
<fieldset id="fieldset">
<h3 align="center">Tenant List</h3><hr/>
name1<br /><hr/>
name2 <br /><hr/>
</fieldset>
</form>
<a id="box-link1" class="myButtonLink" href="category1.php"></a>
</div>
</body>
</html>
My output(main.css) is like this
#output
{
color:#ffffff;
font-size : 20px;
margin : 0;
letter-spacing:1px;
width:480px;
}
I am getting the first name asHumayun,Sahjahan,Bayezid,Bayezid,Asaduzzaman,Mouri in top-left corner. But i like to get the name as list(name1,name2) with link. when i click on a name(name1,name2) it will show details of the name. How can I do this?
Thank in advance
It looks like your looking to iterate the JSON using JavaScript. Since you're using jQuery, you simply need to "iterate" the JSON result. Technically 0 comes before 1 in JavaScript.
var _result = $data[0];
$.each(_result, function (val)
{
console.log(val);
});
http://api.jquery.com/jQuery.each/
Try this:
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM tenanttemp");
$array = array();
while ($row = mysql_fetch_assoc($result))
{
$array[] = $row['TenantFirstName'];
}
echo json_encode($array);
?>

Categories