PHP: Get the value from javascript using getElementbyId - php

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

Related

Create <select> elements on the fly and send multiple POST as an array using jQuery and PHP

I wonder if it's possible to populate <select> elements with option values generated by PHPto a page (after load) whenever a user is clicking a button withid='add'and then be able to store each value selected as an array then send this via $_POST to a PHP page?
I would like to populate the <select> element like this:
/theloader.php
echo "<select class='plan_id'>\n";
while ($row = mysql_fetch_array($course_elements)) {
echo "<option value='".$row['plan_id']."'>".$row['plan_name']."</option>\n";
}
echo "</select>\n";
?>
This will output
<select class='plan_id'>
<option value='1'>Description</option>
<option value='2'>Description</option>
<option value='3'>Description</option>
</select>
Then when the user has populated the needed elements and selected values, click save and send everything to anotherfile.php:
$("#save").click(function() {
// Store each value as a variable then send to /anotherfile.php
});
I lack knowledge in how to perform this kind of action and would really appriciate som tips in the right direction.
Thank's in advance.
Is there any reason the population of the selects has to be dynamic? Does the data change, or are you just letting the user add more line items to choose from pre-set options?
If the data in the dropdown itself is dynamic, you will need a combination of javascript and ajax calls to get the data from the php in real time.
If the options are not dynamic, then you do not need ajax at all, just populate the variables in javascript and create an action to add more dropdowns using the already defined variables.

how to submit combo box value with out form using javascript?

I need to submit combo box value with out form. When I click on any value of combo that suppose to submit automatically using javascript. I am using PHP in backend.
<select name="select" id="select" style="width:50px;">
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="All">All</option>
</select>
Here a js part:
function getinfo() {
var aList = document.getElementById("select");
var val = aList.options[aList.selectedIndex].value;
// document.write("<p>Here what you select: "+val+"</p>");
// here you can send `val` to the server using... are you using any js library?
}
And you need to change your select declaration:
<select name="select" id="select" style="width:50px;" onchange="getinfo()">
--------------------
</select>
OK, how to send val to the server is another question... If you are using jQuery - you can use jQuery.ajax(...) or any helper like jQuery.get(...). If you are vanilla-js user you can use XMLHttpRequest way and if you use any other lib - just check this lib's documentation to get helped about sending data to the server.
You can use the methods below.
1) Use Session or Cookie to send external data
2) Send a POST request using XMLHttpRequest in javascript.Checkout the link here.
3)You can use cURL function to send HTTP POST request. Please go through the link here.
Some comments:
The select element should always be inside a form. But you don't need to expose the form on the page--you do not need to include a submit input element.
You can use Javascript (JS) to submit the form after the user has changed the value of the select.
Or you can use JS with Ajax to submit the form asynchronously in the background--the user will not be aware that the data had been submitted and the page will not reload or "flash"
What would you like to do?

Change php variable based on javascript value

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

extract dropdown boxe's value in php

I have small problem here. I have dropdown box in my form with 2 values; i.e Active and Inactive with Active being selected by default. I want to extract the selected value from the drop down box in PHP. I,m new to PHP and dont know how to do the same. Pls help. Thanks in advance
<select name="your_select">
<option value="active" selected">Active</option>
<option value="inactive"">Inactive</option>
</select>
...
<?php
$isActive = !empty($_POST['your_select']) ? $_POST['your_select'] : null;
?>
Note: The sample above is assuming your form method is post.
When the form is submitted by the user, to your PHP script, you'll find the submitted data in either $_GET or $_POST -- depending on the method of the form.
Using the var_dump() function, you'll be able to see what's in those :
var_dump($_GET);
or
var_dump($_POST);
will show you what's inside those variables ;-)

PHP, Javascript, HTML

How to get option value in PHP?
And i want to redirect to the same page, if option is changed by using onClick() function.
Anybody please help me.
Javascript :
Use eleement_id.selectedIndex() function , It will give the selected option properties.
.......
<select name="select" onchange="document.location.href='this-page-name.php?val=' + this.value">
options....
</select>
You can get its value like this:
echo $_GET['val'];

Categories