<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: PHP runs on the server before the page is sent; JavaScript runs in the browser after the page loads. To get a JS value into PHP after load requires a new HTTP request — either submit a form (POST/GET) or send an AJAX request. This follows the pattern described (hidden input + submit or AJAX) and is what was hinting at with the AJAX suggestion. The original snippet from also had two practical errors to fix: PHP cannot be “set” inside a client-side function, and onchange=change('this.value') passes the literal string "this.value" instead of the selected value (use change(this.value) or this.form.submit() instead). See the PHP docs on form POST handling for how submitted values arrive at the server. ($_POST manual). (php.net)

A minimal non-AJAX approach (no extra JS) is to let the select submit the form when it changes and read the value in PHP. Make sure each <option> has a value (if omitted the text is used). Example HTML + PHP:

<form method="post" action="">
  <select name="customer" onchange="this.form.submit()">
    <option value="">--choose--</option>
    <option value="Varma">Varma</option>
    <option value="Phani Varma">Phani Varma</option>
  </select>
</form>

<?php
echo htmlspecialchars($_POST['customer'] ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
?>

The <option>/<select> behavior and value attribute are documented here. (developer.mozilla.org)

A modern AJAX route uses the Fetch API: send the selected value to a small PHP endpoint that echoes or returns JSON. Example JS + server:

function change(val) {
  fetch('receiver.php', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: 'customer=' + encodeURIComponent(val)
  })
  .then(r => r.text())
  .then(text => console.log('server:', text))
  .catch(console.error);
}

In receiver.php read $_POST['customer'] (or use php://input for JSON) and always sanitize before output or DB use (e.g., htmlspecialchars() or filter_input() / filtering). Links: Using Fetch API, htmlspecialchars() docs, filter_input() docs. (developer.mozilla.org)

Quick troubleshooting checklist: confirm the select has a name, confirm option values, verify the JS actually receives the value with console.log, inspect the Network tab to see the request, and confirm the PHP endpoint receives the parameter (dump $_POST) and applies proper escaping.

Recommended Answers

All 3 Replies

place a value attribute inside option tag

<?php $name=?>

This piece of code will generate an error since it is not complete.

To pas a JS function value to PHP the JS function should return the value first. Then the value could either be
1. set into a form element (maybe a hidden input) and the form posted to the server or
2. sent to the server by ajax

Hope I understood the question. Could you explain more what you want to achieve, please.

Yea you should probably use AJAX for that :). That's an Asynchronous Javascript And XML call, which enables you to execute PHP even after the page has loaded. Check out how it works here:

A tutorial at w3schools:
The jQuery documentation: http://api.jquery.com/jQuery.ajax/

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.