Hi,

What happens? You type something in a text field, and based on the input, you should get some suggestions. Now, the problem is that I can get the info out of my database with PHP, but now I need to be able to use the PHP array in javascript. So I guess that needs to be done with ajax, but I don't know how I should do that.

The part with the XMLhttp request isn't the problem. The problem is that I don't know how to make my function to transport my PHP array to javascript. Can anyone help me out, or send me in the right direction?

My php array is like $gemeente = ("aalbeke", "aalter", "aalst","aarschot");

Any ideas?

You can try this one, but don't trust my PHP syntax.

<script type="text/javascript">
<!--

var jsArray = [ ];

<?php
$phpArray = new Array("pOne", "pTwo", "pThree");
   foreach( $phpArray as $toJsArray => $convert ) {
   echo "jsArray[ $toJsArray ] = $convert";
   } ?>
// jsArray now hold items from the phpArrays.

   for ( var x in jsArray ) {
   alert( jsArray[ x ] ); }
// -->
</script>

hope it helps...

Hi,

I don't think that is possible, since the array is adjusted by the input of the user. that is why I need to use ajax. There input can change all the time. So if they for example type:

a

Then a list should show up(that will be formed by an array):

$gemeente = ("[B]a[/B]alter","[B]a[/B]albeke","[B]a[/B]ntwerpen","[B]a[/B]nderlecht");

And when they type further:

an

Then the list should be adjusted(so a new mysql request should happen so that the array is adjusted):

$gemeente = ("[B]an[/B]twerpen","[B]an[/B]derlecht");

So I need a function so that ajax activate my php file, to make an array with the possible suggestions, and when it is formed, it can be used by javascript.

So the problem is that I don't know how to this. (the php part and mysql isn't a problem, that is working right).

So any ideas on how I could achieve this?

Oh, i'm sorry about that post, i thought that it was just, not-so complicated arrays...

It's really hard for me to provide solution's that involves PHP syntax, the main reason is, that i can't perform basic run on the script.

But don't worry i'll figure out a way to help you out on this one.

I'll just post back when i get my solution.

- Or may be someone else will provide you with the exact solution that you need..

Maybe the PHP script can help you a bit. This is how it looks:

<?php

$q=$_GET["q"];

$con = mysql_connect('localhost', '*****', '******');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("vetweb", $con);

$sql="SELECT * FROM locatie WHERE gemeente LIKE '".$q."%' LIMIT 20";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
  $gemeente =  $row['gemeente'] ;
  $postnummer = $row['postnummer'] ;
}

mysql_close($con);

?>

Hi

In PHP generate the collection of data and for the "transport" you can use any HTML tag( ie textarea, select, ... ) and for render in the client extract the collection from the HTML of ajax.

PHP
<?php
$phpArray = new Array("pOne", "pTwo", "pThree");
   foreach( $phpArray as $toJsArray => $convert ) {
   echo "<textarea name='transport' class='invisible' >$convert</textarea>";
   } ?>

And in JS ( client )

var data = document.getElementsByName( "transport" );
      if( data != null ){
        for(j=0; j < data.length; j++){
// you have the data in the client for your array
          eval( alert( data[j].innerHTML ) ) ;
        }
      }

Final Note: The textarea have a class = 'invisible' that means display:none

Please for the love of all that is good don't use Jodin's example.

echo json_encode($your_php_array);

Done.

Member Avatar for diafol

Listen to Shawn! However, am I right in thinking that it only works with php5.2 and above? This function is especially useful for ajax when a set of values need to be returned from a php script. I think there's a module for previous php versions.

Listen to Shawn! However, am I right in thinking that it only works with php5.2 and above? This function is especially useful for ajax when a set of values need to be returned from a php script. I think there's a module for previous php versions.

It's default in 5.2 but is a PECL extension that can be used in older versions (Check http://pecl.php.net for details about it)

For a non-json approach, then you could try this .... I just did and it worked :

[B]Server-side (PHP)[/B]
$gemeente = ("aalter","aalbeke","antwerpen","anderlecht");
echo implode(',', $gemeente);
exit;

[B]Client-side (Javascript)[/B]
function AjaxResponseHandler(response){
	var myVar = response.split(',');
	alert(myVar);
}

Of course, you must choose a delimiter (I used comma) that will never appear in your strings. For safety you could delimit with some contrived string, eg. "£$%^&" (also tested). So long as PHP implode()s and javascript split()s with the same string you'll be OK.

I guess that json's internal workings are similar.

Airshow

Thanks for the multiple solutions, I will look in to them :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.