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
Related
I would take the value of the combobox by using getelementbyID to PHP variable,
And The Code Like This
<select name="PROPINSI" onchange="document.getElementById('KOTAA').value = this.value" >
<option value="a">a</option>
<option value="b">b</option>
</select>
<select name="KOTA" id="KOTAA">
<?php
$propinsi = ; //in this variable, I want take this value
$sqll="SELECT kota FROM master_propinsi WHERE propinsi = '$propinsi'";
$hasill= mysql_query($sqll);
while($dataa = mysql_fetch_array($hasill))
{
echo "<option value='$dataa[kota]'>$dataa[kota]</option>";
}
?>
</select>
Can SomeOne Help Me?
I really appreciate your answer
Thanks
How to get value From Combo Box with ID="KOTAA" to variable $propinsi
You cannot pass variable values from the current page javascript to the current page PHP code... PHP code runs at the server side and it doesn't know anything about what is going on on the client side.
You need to pass variables to PHP code from html-form using another mechanism, such as submitting form on GET or POST methods.
You can't put javascript code in PHP. Although, there is an alternative: Ajax
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.
I'm fairly new to jQuery and OOP in PHP,
but what I have is a PHP Object that has an array
$array[0] = "a"
$array[1] = "b"
$array[2] = "c"
$array[3] = "d"
and a select box in my HTML
<select class="box">
<option value="1">First Letter</option>
<option value="2">Second Letter</option>
<option value="3">Third Letter</option>
<option value="4">Fourth Letter</option>
</select>
<div></div>
How do I dynamically change whatever is in the div tags as someone changes the value of the select box using jQuery?
Is there a way to do something like
$('.box').change(){ var value = $object->array[index-1]}?
Assuming I had already have the value for index.
EDIT
I know I'll have to use Ajax, but I'm not sure how to call a method in an instance of an object already made.
You can't access PHP through javascript. There are several possible solutions:
Load all of the possible data combinations at once onto the page stored somewhere in the DOM. As the user cycles through the options, change them appropriately.
Load only the initial elements onto the page without javascript, then use Ajax to load all other possible combinations.
Load each combination with ajax on the fly.
You can either have javascript make an Ajax request to the same page and filter the request, or you can create a proxy that will serve only the requests you need when hit with ajax.
Remember that you are trying to combine PHP with Javascript.
Your code will primarily be written in javascript, but you'll
only inject bits of PHP code into the javascript code.
As in this instance, use the standard jquery onchange event method 'change()'
for the selectbox, set the javascript variable value to $object->array[0]
from PHP.
$('select').change(function() {
var value = <?php echo $object->array[$index] ?>
$('div').html(value)
});
You can use the php function json_encode to turn your php data into a javascript object. e.g
<script type="text/javascript">
var mydata = <?= json_encode($myarray) ?>;
console.log(mydata);
</script>
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.
** 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!