This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I programatically set the value of a select box element using javascript?
I'm trying to update a table with the results of a query when a select option has changed. The following test code works fine. But when I try to update the query, it doesn't work. I'm guessing it has to do with the 'echo', but I cant get figure it out. Thanks.
<script type="text/javascript">
function ChangeText(value)
{
document.getElementById("p1").innerHTML=value;
}
</script>
<select onchange="ChangeText(value)">
<option value="ONE">one</option>
<option value="TWO">two</option>
<option value="THREE">three</option>
<option value="FOUR">four</option>
</select>
<?php
$variable1 = '<p id="p1">place holder</p>';
echo $variable1;
?>
what doesn't work:
document.getElementById("country").innerHTML=value;
$country = '<p id="country">United States</p>'
$result = mysql_query("SELECT * FROM statistics WHERE (Country='$country')");
The result of that is a blank table when the page loads, I would have expected 'United States' to be the default value. And when I select something from the pulldown, it does nothing.
PHP is executed on the server-side, before the user ever sees the page. So once the user is looking at the page, PHP is done executing.
So you can't "update a PHP variable with Javascript, because Javascript runs on the client-side, which happens after PHP has completely finished executing.
You should look into AJAX.
I think you have some confusion about how server-side scripting vs javascripting works. PHP code does not run on the client side. PHP code is executed by your server, and the results (everything echoed + HTML markup) is sent to the client's browser. If you want to dynamically update a page you would have to use a form and reload the page, or use AJAX.
Related
how can i change/assign a value to php variable depending on a javascript variable?
here is my code.
<select id="reOutcome" name="reOutcome" onchange="OnChange(this.form.reOutcome);">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
<script type="text/javascript">
function OnChange(dropdown)
{
var myindex = dropdown.selectedIndex
var SelValue = dropdown.options[myindex].value
if(SelValue==2){
<?php
$sCOMPFields .= "|"."SreComments";
$sCOMPFields .= "|"."rePrID";
?>
}
return true;
}
</script>
The onchange function is working fine. I just don't know how to change the php variable. I searched online a lot. All im getting is how to assign php variable to javascript. That is not what im looking for.
Thanks for your help.
The execution you want won`t occur, because the flow of the php scope and the javascript scope occurs on different moments. It is something like this:
So, you can`t execute php while the javascript is being executed on the computer of the user through the browser, but you can execute php on your server to generate the javascript you need to be executed on the user computer.
Actually, your question seems to be closer to a "what is the best way to do (something)"
PHP variables are set at run time. You can't do it directly within the javascript. Off the top of my head the best way I can think of to do it would be to set the php variables as session variables. Then use your javascript to call a php file via ajax/jquery that can update the session variables.
that's exactly we use ajax.
make the page loads in default preferences, then update it using ajax
I have a page with php and javascript. In the php section i need to call a function with an argument $arg, Now $arg is the problem, $arg is actually value of a dropdown option in the same page . Here is what i am trying to accomplish.
<?php include("aclass.php")?>
<script language="javascript">
function getoption(val){
}
</script>
<html>
<select name="sel" onchange="getoption();">
<option value="1">One</option>
<option value="2">Two</option>
</html>
<?php
$object=new a;
$object->calculate($arg);
?>
// Here the issue is $arg should be the value i select in dropdown, $arg should be 1 if i select One and should be 2 if i select Two in dropdown.
" Is there a possibility to pass the javascript value 'val' to php variable $arg "
The thing you want to do is not possible without a form submission or without a contact to the server. PHP is a server side language whereas javascript is client side. So obviously you cannot do a server function in the client side.
So, either make this as a form and submit the form on change of the value in select or if you dont want reload of the page, try using ajax
You can use PHP variables in Javascript but the opposite can't be done. PHP runs server-side. Javascript is client-side. The only way to send something from Javascript to PHP is using AJAX. You might want to search in that direction.
I want to display the photos according to the album selected. But, I don't want to post the page, I want to just change the div.
This is my script:
<script type="text/javascript">
function replaceContent(divName, contentS) {
document.getElementById(divName).innerHTML = <?php echo get_pictures_from_album($fb, $albums, contentS); ?>;
}
</script>
And this is the select tag that invokes it:
<select name="album" size= "1" style="width:210;" onchange="replaceContent('photos', this.options[this.selectedIndex].value);">
<?php get_albums_select_list($albums); ?>
</select>
<div id = "photos">
<?php echo get_profile_pictures($fb, $albums); ?>
</div>
I understand from a reading that I have done that the problem might be connected to javascript Vs php variable types.
Please advise.
Looks like you are looking for an AJax call to an PHP script that retrives the data for the appropriate album selected and THEN update the div with the callback function.
Ajax + PHP basics
You are mixing Clientside and Serverside Code here. The function replaceContent is called after the page (and the php code) was loaded. You would need an Ajax Call for that if you need more information about that:
Ajax Tutorials on Google
What you are doing is not possible because PHP code runs before (on the server because PHP is server-side language) javascript code.
You will have to resort ot AJAX for that.
I'm trying to populate a second dropdown in a dynamic way based on a previs selected dropdown.
However, I've managed to write get the page to reload when I choose anything in the dropdownbox but the chosen value isnt passed after reloading.
I have register_globals turned off (and prefer to) and i'm using the GET function to submit the form. However when I try setting values in the URL I cant get it to work.
Example: dropdown.php?area=1 still gives me a value in the dropdownbox with the default value.
What am I doing wrong? Running on a LAMP server. Apache 2.2, php 5.3.
Note: I found the php code here on the web wwich is suppose to help me pass the GET variable and select the option in the selectbox.
This is my code:
<html>
<head>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.area.options[form.area.options.selectedIndex].value;
self.location='dropdown.php?area=' + val ;
}
</script>
</head>
</body>
<? #$area=$HTTP_GET_VARS['area']; ?>
<form action="" method="get">
<select name="area" id="area" onchange="reload(this.form)">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>
</body>
</html>
Also, if this could be done with POST (or equivalent function) it would be even better.
Regards.
I think you're not specifying anywhere which one of the options should be selected on page load. Depending on the value of $area, you should add something like
<option selected>1</option>
You could easily do this with a couple of lines of PHP when rendering the option nodes:
<? if $area == 1
print '<option selected>1</option>';
?>
etc.
Alternatively, you could just populate the second combo using client-side javascript eliminating the need for a page reload. If you need to do some sensitive server-side processing to calculate the value of the second combo, do it in a background AJAX call using jQuery (examples here). Postbacks for this kind of thing are kind of undesirable and old-fashioned these days.
Regarding the GET issue, if submitting the form has any side effects (eg. a change in state in the user's account, deleting something, creating a new entity) then it should definitely be a POST. Discussion here for example.
** EDIT **
I'm afraid I wasn't in the right direction - the problem isn't what I asked about.
the Javascript works as it should, it's the PHP that doesn't show what I want it to
on the first "run" - and I'm stil not sure why.
Sorry for somewhat wasting your time...
**
I have a form that might or might not already contain data in its fields.
If a user reaches the form and he already has some data in the system,
his ID number, for instance, will appear in the ID field - and a JavaScript function running
onLoad will disable the input field and change its style:
<body onload="runFunction()">
the problem is that it seems to run before the PHP does its part,
and only if I refresh the page, the JS function does everything I want it to
(it disables and styles some other form fields that depend on a PHP condition).
Is there a way to have this JS function run AFTER the page was rendered?
I tried running this before closing the page:
<body>
...
...
<script>
runFunction();
</script>
</body>
</html>
but to no avail.
any ideas?
Thanks!
some of the relevant PHP code: [I removed id attributes to make reading easier]
<?php if ($h_phone != '' && $h_phone_pre != '') {
echo "<td class=\"input\"><input type=\"text\" id=\"new_user_home_tel\" value=\"$h_phone\" size=\"8\" maxlength=\"7\" disabled=\"disabled\" /> -
<select id=\"new_user_home_tel_pre\" disabled=\"disabled\">
<option value=\"$h_phone_pre\" selected=\"selected\"></option>";
} else {
echo '<td class="input"><input type="text" id="new_user_home_tel" size="8" maxlength="7" /> -
<select id="new_user_home_tel_pre">
<option value=" " selected="selected"></option>';
}?> <option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="08">08</option>
<option value="09">09</option>
</select>
</td>
the Javascript code just changes the style if the field isn't empty and that works,
but the PHP works only after a refresh.
Your Javascript does run after the page is loaded, the problem is elsewhere.
It must be that your server sends different HTML before and after refresh. I suggest that if you save the source of the page you get first time and compare it with the source you get after refresh. I bet you will spot the difference and that will tell you what is wrong.
JQuery does this. Anything inside the following will execute once the page is rendered:
$(document).ready(function() {
//Call your function here
});
Try the "defer" attribute.
One way is to output-buffer the page in PHP. That means everything generated goes into memory until the script finishes running and then it's all sent out at once.
Take a look at http://uk.php.net/ob_start
the non JS library answer is:
<script>
window.onload = function(){
/* do stuff */
}
</script>
However using a JS library like JQuery will take care of all those niggle cross browser bug/problems.
Your right to place your <script> blocks at the end of you body. This improves page load performance as the browser blocks when it hits a <script> block.
Definitely hacky, but you could always put a 2x2 transparent image below your form and run the function using it's onLoad. I did a similar thing on a conditionally included file so there were no errors in the case that the file wasn't included.
I'd suggest that you add your content to an output variable and than echo it at the end of the file. Ex:
...
$output .= '<body onload="runFunction();">';
while(...)
{
$output .= '...';
}
$output .= '</body>';
...
echo $output;
I'm afraid I wasn't in the right direction - the problem isn't what I asked about. the Javascript works as it should, it's the PHP that doesn't show what I want it to on the first "run" - and I'm stil not sure why. Sorry for somewhat wasting your time...
I know this is really late for this post, but the reason this isn't working is that PHP renders HTML and then you can't interact with it again (unless you can do some snazzy AJAX stuff). Point being that the JS works fine bc it is client side, PHP doesn't work bc it is server side. Hope that helps anyone else who comes here!