Welcome to WebmasterWorld Guest from 54.167.58.159

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

passing an array from php to javascript

i have used the following code in php

     

anya

4:56 am on Aug 21, 2013 (gmt 0)



[size=4][/size]
<?php
$dbcon = mysqli_connect('servername', 'username', '', 'dbname');
$sqlget = "SELECT * FROM userdata";
$result = mysqli_query($dbcon, $sqlget) or die('error getting');
$image = "vbm1.png";

echo "<img src=".$image." Style=width:350px;height:100px;>";
echo "<table>";
echo "<tr><th>name</th><th>email</th><th>phone</th><th>city</th><th>subject</th><th>message</th><tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
echo $row['phone'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
echo $row['subject'];
echo "</td><td>";
echo $row['message'];
echo "</td></tr>";
}
echo"</table>";
?>

robzilla

11:14 pm on Aug 27, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



What's your question, exactly? I don't see any Javascript.

By the way, you do not need to use this many echo commands. This will work just as well:

<?php
$dbcon = mysqli_connect('servername', 'username', '', 'dbname');
$sqlget = "SELECT * FROM userdata";
$result = mysqli_query($dbcon, $sqlget) or die('error getting');
$image = "vbm1.png";

echo "<img src=".$image." style=width:350px;height:100px;><table><tr><th>name</th><th>email</th><th>phone</th><th>city</th><th>subject</th><th>message</th><tr>";

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr>
<td>$row['name']</td>
<td>$row['email']</td>
<td>$row['phone'];</td>
<td>$row['city'];</td>
<td>$row['subject'];</td>
<td>$row['message'];</td>
</td></tr>";
}

echo "</table>";
?>


When you use double quotation marks to echo something, PHP will check everything between those quotes for variables to parse. It will not do this when you use single quotation marks. To further illustrate, all three of the below examples give the same output:

<?php
$var = "cruel";

// #1
echo "hello ";
echo $var;
echo " world";

// #2
echo "hello " . $var . " world";

// #3
echo "hello $var world";

// This won't work
echo 'hello $var world';

?>


Also, note that <th> is not supported in HTML5. Instead, you could use <tbody> and <thead>.

swa66

8:57 am on Aug 28, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Also, note that <th> is not supported in HTML5. Instead, you could use <tbody> and <thead>.

Unless you can show a part of the spec of HTML5 that says that's true, I'd call that factually false.

<th> is still defined here:
http://www.w3.org/TR/html5/tabular-data.html#the-th-element
http://www.whatwg.org/specs/web-apps/current-work/multipage/tabular-data.html#the-th-element

As a matter of fact the examples in the html5 spec use <th> extensively:
http://www.w3.org/TR/html5/tabular-data.html#examples
http://www.whatwg.org/specs/web-apps/current-work/multipage/tabular-data.html#examples-0

[not linked intentionally: You need the #parts ...]

robzilla

11:25 am on Aug 28, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



You're absolutely right, swa66. I must have misread or misinterpreted this, wherever I read it, or my memory failed me last night and wrote the nonsense. Should fact-check.

I cannot edit my post, unfortunately, but please consider it retracted. Sorry about that.

swa66

11:37 am on Aug 28, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



All I know is that to write correct polyglot html5 (xhtml5 AND html5 at the same time), you must add a <tbody> of <thead>/<tfooter> around every <tr>. Got to do with browsers being "helpful" and adding back the missing elements in the DOM if you fail to add them yourself AFAIK.

anya

9:01 am on Sep 13, 2013 (gmt 0)



Actually...i need to access database data dynamically in frontend based on search. have accessed database data in php,...how to display this table data with formatts in front end...

As i am new to ajax,php....suggest me better way...

have an idea to use XML...is it right?

robzilla

11:47 am on Sep 13, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Look into XMLHttpRequest, there are plenty of examples and tutorials available online.

It's called "XMLHttpRequest" but it doesn't necessarily use XML; you can use it to simply display the output of your PHP on your webpage.

anya

5:57 am on Sep 14, 2013 (gmt 0)



I have used xmlhttprequest to display the php file(this retrieve MYSQL database) ..But the data is without any formatting....need it to display with some images, formats and also grids.....

robzilla

9:56 am on Sep 14, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Whatever the output of your PHP script is, will be displayed on the HTML page if you retrieve it via XMLHttpRequest. If you need additional formatting, can't you simply change the output of your PHP script?

anya

9:47 am on Sep 16, 2013 (gmt 0)



is it possible to get the output in html as follows, by simply editing on php output(which i get from above code)........in each grid i need separate company details that is accessed from database and if user needs ,they should send enquiry

hugoramirez

2:30 pm on Sep 21, 2013 (gmt 0)



jQuery makes AJAX a lot less painful:

$('#somediv').html('Loading results, please wait.');
$('#somediv').load('search.php', { 'q' : $('input[name=q]').val() });

For the "lazy" (aka a smart programmer). That expects the server to spit out valid HTML into the DOM.

If you want to get fancy and API-like, you could build a PHP array of results and output the array back to the browser via json_encode(). Then Javascript handles the HTML generation. It really depends on what you want to do, but for the majority of cases, the above is pretty sufficient.

penders

2:15 pm on Sep 22, 2013 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



...you must add a <tbody> of <thead>/<tfooter> around every <tr>


Around groups of TRs, not literally every TR.

Just going back to the original question, if you aren't using AJAX, then you must "print" the relevant JavaScript to the page using PHP in order to pass the data from PHP to JavaScript...

<?php 
$myVar = 'Hello World';
echo <<<EOD
<script>
alert($myVar);
</script>
EOD;
?>

tw56

4:38 pm on Nov 4, 2013 (gmt 0)



If you still need it, see if this will help.

<?php
include('code/connect.php');
include('code/filter.php');
$key = strip_tags($_GET['key']);
$kv = mres($key);
$gw = 'gword';
$ew = 'eword';
$top = 'topic';
$com = 'comments';
$ex1 = 'ex1';
$ex2 = 'ex2';

$sql = 'SELECT * FROM `words` WHERE `key` = \''.$kv.'\'';
$query = $sql;
$result = mysql_query($query);
$rows = mysql_num_rows($result);
if($result) {
for($i = 0; $i < $rows; $i++) {
$f1[$i] = mysql_result($result,$i,$gw);
$f2[$i] = mysql_result($result,$i,$ew);
$f3[$i] = mysql_result($result,$i,$com);
$f4[$i] = mysql_result($result,$i,$ex1);
$f5[$i] = mysql_result($result,$i,$ex2);
$h[$i] = '<p class="one">' . $f1[$i] . ' >> ' . $f2[$i] . ' " ' . $f3[$i] . ' " ' . $f4[$i] . ' " ' . $f5[$i] . '</p>';
echo $h[$i];
}
}
?>

Just modify the formatting if you want tables in the $h line.