I retrieved all of my friends' names which are currently online using MySQL joins and PHP.
Now I want to insert their names inside JavaScript for chatting with them.
Friend1
Friend2
I am unable to do this. Because I don't know how to call JavaScript inside PHP or viceversa.
while($get_usernames=mysql_fetch_array($get_users_query)) {
echo '<br>'.$get_usernames['username'];
}
I want to show each of $get_usernames['username'] as Friend1, Friend2, inside JavaScript
You can use AJAX to call a php page and get value from that.
Call the php page and after querying simply return value using echo. You can get that value using responseText in javascript
http://www.tizag.com/ajaxTutorial/ajaxphp.php
If you want to place some PHP variable inside javascript, a simple way of doing it is by simply using the php loop inside your page to create the a tags.
Something like this:
$aTagList = "";
while($get_usernames=mysql_fetch_array($get_users_query)) {
$aTagList .= "<a href='javascript:void(0)' onclick='javascript:chatWith(\"".$get_usernames['username']."\")>".$get_usernames['username']."</a>";
}
Then echo the $aTagList wherever you need it in the page with some inline code.
<?php echo $aTagList; ?>
Not sure if I got all the escaping correct, but it should give you the right idea.
Something like:
<?php
$aUsernames = array();
while($get_usernames=mysql_fetch_array($get_users_query)) {
array_push($aUsernames, $get_usernames['username']);
}
foreach($aUsernames as $name) {
?>
<?= $name; ?>
<?php
}
?>
Using AJAX is much neater ofcourse, but this will suffice if it is all in one PHP file.
Also I would like to encourage you to move to PDO or Mysqli since mysql functions are deprecated.
Related
Ok so I have the code for a form that is called and works but it needs two varibles grabbed from the string of a url. I have the first and the second is the same for what im doing on any page that I am creating which is alot. Here is the code at the url: collabedit.com/9g99j
Question if Get <?php echo $_GET['id']; ?> is grabbing my id string from the url how do I use this in the echo of my function I just defined at the bottom of the code? Instead of having this: echo DescriptionGet(1256124, 50874); can someone tell me how to put something like this: echo DescriptionGet(1256124, $id);
This would make it so i dont' have to enter that id value for every page I want to create.
Thanks,
Thanks everyone for your replies and I was able to figure it out on my own and actually used exactly what the first reply was.
Now I have a new question about this function. How do I make it grab the image from that same page its grabbing the form code from? I can't figure this part out and its keeping me from doing mass automation for this site.
Anyone help?
Try this:
$id = $_GET['id'];
echo DescriptionGet(1256124, $id);
You can change your function definition from:
function DescriptionGet($c, $id)
to
function DescriptionGet($c, $id=50874)
Each time when you will call DescriptionGet($c) it will behave as you passed $id=50874 but also if you need you can call DescriptionGet($c, 20) and $id in the function will be set to 20.
And in case you want to simple use $_GET['id'] as function parameter you can simple run
echo DescriptionGet(1256124, intval($_GET['id']));
you don't even need to use extra variable.
Im trying to echo an xml value to an dynamic created div tag, but i cant seem to find any information about it.
The DOM tree is created with JavaScript
textArea = document.createElement('textarea');
The output become <textarea></textarea>No problem here...
Then Im getting the value from an xml file with simple php.
<?php
foreach($xml->sticker as $sticker ){
$post = $sticker->text . "</br>";
$post;
echo $post;
}
?>
and the echo returns " Hello World "
Now to the issue, how do I echo inside the dynamically created textarea
the output should be
<textarea>Hello world</textarea>
Any ideas ? or a link maybe ?
Thanks
If you successfully load the xml with XHR then you can put your response inside your textarea with:
textArea.innerHTML = responseFromXHR;
It's not quite clear on how your application is supposed to work. If you want to fetch the data using ajax or not. Some people here seems to think so although you haven't stated that. I will assume that is not the case. If it is the solution would most likely be to have you PHP script return JSON encoded data, decode that using Javascript and insert it into the textarea.
Since you dynamically create the textarea you also have to dynamicly fill it with text using Javascript. Just add the text to the .innerHTML of the textarea.
Some if check in PHP to make sure the text should be printed and that the textarea exists and then using PHP add this to a Javascript.
echo 'textArea.innerHTML = "' . $post;
If you are supposed to print all posts in the same textareayou could do something like
<?php
echo '<script>';
echo 'textArea = document.createElement(\'textarea\');';
foreach($xml->sticker as $sticker ){
$posts .= $sticker->text . "\n\r";
echo $post;
}
echo 'textArea.innerHTML = "' . $posts;
echo '</script>';
?>
You could also have the check for the textarea's existence in Javascript if you'd like that. You probably wan't to do some filtering on the text you put into the textarea though.
How are you getting the value from the XML file?
In the PHP, return a string or JSON object, then in the AJAX callback, insert the data into the desired location.
Your php script that returns "Hello World" must be called via AJAX and on successful answer just like 'apelsinapa' said:
textArea.innerHTML = responseFromAjaxCall;
Otherwise - the PHP code gets parsed by the server before JavaScript creates the textarea node.
I made some javascript code to view the text inside input text. When I click on the submit with this code:
alert("The field contains the text: " + frm.find.value)
This works, so I want use this part from javascript in php "frm.find.value"
for example: I'll write the wrong code as i used it:
$j='<script>+ frm.find.value<script/>';
$code = mysql_query("SELECT * FROM s_output WHERE name='$j'");
while($rowc = mysql_fetch_array($code))
{
$code=$rowc['code'];
}
echo $code;
This code for view code number using the name from database. i can use it with only php, but in my example I cant because I have an error with charset UTF-8, SO i just need use the code or numbers.
Thanks
you have right idea... use an ajax call to your php page where the php page echoes the result. Here is a not-so-elegant example to demonstrate, http://www.w3schools.com/php/php_ajax_database.asp
I am trying to generate a combo box with values of my database in it.
To do so I decided to put a simple php code inside my .html.twig
Here is the code
<?php
$villes = $this->getDoctrine()
->getRepository('ProjetEsamuzeDiorBundle:Villes')
->findAll();
echo "<select name='ville'>";
for ($i=0;$i<count($villes)-1;$i++)
{
echo "<option value='".$villes[$i]->getId()."'>".$villes[$i]->getNom()."</option>";
}
echo "</select>";
?>
The output of this is
getDoctrine() ->getRepository('ProjetEsamuzeDiorBundle:Villes') ->findAll(); echo ""; for ($i=0;i".$villes[i]->getNom().""; } echo ""; ?>
Keep in mind this is directly on the page, as if there was an echo just before, but there isn't. I thought maybe it's the $this, but replacing it with $villes gave the same result
Also I don't see the for what I see is exactly this
getDoctrine() ->getRepository('ProjetEsamuzeDiorBundle:Villes') ->findAll(); echo "(combobox is empty here)"; ?>
What am I doing wrong?
You cannot use PHP in twig templates. Twig is parsed and converted to PHP but no PHP code is interpreted. You have to use twig syntax.
Furthermore, you really shouldn't put logic into templates. Twig is doing great job preventing you from doing that. Database query should go to the controller.
Shouldn't it be $villes[$i] instead of $villes[i] ?
Use the entity form type for this task.
I am working on a page that displays all state parks. Something like this:
http://www.comehike.com/outdoors/state.php?country_id=1&state_id=6&state_name=California&country_name=
I have the park data, including lat/lng in my database and I already get that data during the request. What I want to avoid is doing an AJAX call which will query the park data again.
Is it possible for me to just send the PHP result set to the JS somehow and to loop through the parks and display them?
I am already doing something like that by sending the map-center lat/lng so I figured it is doable with a result set.
Thanks,
Alex
This depends on how your pages are setup. If you're using some kind of MVC setup, you need to pass the variable to your view first. Or if it's a single page, just retrieve the PHP above and pass it as a variable.
<?php
$parkData = $model->getData(); //make sure this returns an array or an object array or something
$parkDataJson = json_encode($parkData);
?>
//in your view
<script type='text/javascript'>
let parkData = <?php echo $parkDataJson ?>;
//now you have a JSON array available for your use
</script>