<html>
<head>
<script>
function change(x)
{
var str=x;
<?php $name=?>str;
}
</script>
<?php echo $name;
</head>
<body>
<form>
<select name="customers" onchange=change('this.value')>
<option>Varma</option>
<option>Phani Varma</option>
<option>Prabhas Varma</option>
<option>Kishore Varma</option>
</select>
</form>
</body>
</html>
so when ever user change the select,the value should be passed to php & it must be echoed by php.Lots of thanks To all

Dani AI

Generated

Short answer: you cannot directly assign a PHP variable from client-side JavaScript after the page has been sent. PHP runs on the server and builds the HTML before the browser runs any JS. To make PHP see a value chosen in the browser you must send it back to the server — for example by submitting a form (GET or POST) or by an asynchronous request (AJAX/fetch). Also be careful not to pass a quoted literal when you intend to pass the element's value — the browser will send the literal string instead of the selected value.

A simple, reliable approach is to submit the select inside a form and read the value in PHP. Example (client + server):

<form method="post" action="">
  <select name="customers" onchange="this.form.submit()">
    <option value="Varma">Varma</option>
    <option value="Phani Varma">Phani Varma</option>
    <option value="Prabhas Varma">Prabhas Varma</option>
    <option value="Kishore Varma">Kishore Varma</option>
  </select>
</form>
<?php
$sel = filter_input(INPUT_POST, 'customers', FILTER_SANITIZE_STRING);
if ($sel !== null) {
    echo htmlspecialchars($sel, ENT_QUOTES, 'UTF-8');
}
?>

If you prefer no page reload, send the value to a PHP endpoint via fetch/XHR and update the page with the response. If using a query string (document.location.href), remember to URL-encode the value.

Notes and troubleshooting

  • : attaching onchange to the select is fine — just ensure you pass the actual value, not a quoted literal.
  • : redirecting with a query string works, but use encodeURIComponent and consider POST or fetch for cleaner URLs and larger payloads.
  • Always validate and sanitize server-side (never trust client input) and escape output with htmlspecialchars. For DB work, use prepared statements or whitelists for allowed values. For quick debugging, dump $_POST/$_GET to verify what the server receives.

Recommended Answers

All 2 Replies

place on the onchange attribute to the option tag attribute and on the option tag attribute place s value attribute..

<select name="selected"><option value="first value" onChange="change(this.value)">first value</option></select>

or you can have it this way

<select name="selected" onChange="change(this.value)"><option value="first value" >first value</option></select>

either way it will be the same

hai Venter,

can you explain me a little bit more about whats your actual requirement is?

i am getting little bit confustion about your requirement.

so i have modified your code what i understood from the post which is given below

have a look at once. after changing the valus of drop down box the page is redirected to Requirement.php page

there you can echo your selcted value this is also given in the below of the code.

<html>
<head>
<script type="text/javascript">
function change(select_id)
{
    // getting values into java script from the dropdown box in html

    var CustomersSelect = document.getElementById(select_id); 
    var customer_name = CustomersSelect.options[CustomersSelect.selectedIndex].text;

    document.location.href ="Requirement.php?my_val="+customer_name;
}
</script>
</head>
<body>
<form>
    <select name="customers" id="customers" onchange="change(this.id)">
        <option>Varma</option>
        <option>Phani Varma</option>
        <option>Prabhas Varma</option>
        <option>Kishore Varma</option>
    </select>
</form>
</body>
</html>

at Requirement.php you need to get the selected value by using $_GET['my_val'] for echoing the value

let me know whether this is what you expected or not

happy coding

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.