1

I have a php variable called $photographerNum that i want the value of to change depending upon value in my drop down field on my page, what would the javascript look like for this as well as how to receive the new value to update the php variable?

    <!DOCTYPE html>
<html>
<body>
<? $photographerNum = 1; ?>
<p>Select an item from the list.</p>
<select id="photographerNum" onchange="myFunction()">
  <option value="Item1">Item1
  <option value="Item2">Item2
  <option value="Item3">Item3
  <option value="Item4">Item4
</select>
<script type="text/javascript">
function myFunction() {
    var itemSelected = document.getElementById("photographerNum").value;
    document.getElementById("demo").innerHTML = "You selected: " + itemSelected ;
}
</script>
<p id="demo"> - photoNum=<? echo $photographerNum ?></p>
</body>
</html>
5
  • By the time the user is selecting something from a dropdown, the PHP script is all finished. What do you mean by changing the PHP variable? Commented Feb 24, 2017 at 21:41
  • before the dropdown the php value is 1 after the drop down is selected the php variable might be changed to 2 or 3 i'm echoing the variable below the drop down Commented Feb 24, 2017 at 22:59
  • When the user is viewing the form in the browser, the variable doesn't have any value, because the script isn't running. Commented Feb 24, 2017 at 23:02
  • isn't that what the function is for? Commented Feb 24, 2017 at 23:11
  • The function is changing the DOM, not a PHP variable. Commented Feb 24, 2017 at 23:15

1 Answer 1

2

Try this, when you select a new item a form is triggered which outputs the value of the selected item, after that, you can send the variable and process it in server-side.

<?php 
$photographerNum = 1;
 echo 'Initial value photographerNum::' . $photographerNum;
?>  
 <form name="myform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
   <select name="photographerNum" id="IdphotographerNum"   onchange="myform.submit();">
    <option value="0">Selecte an item
    <option value="1">Item1
    <option value="2">Item2
    <option value="3">Item3
    <option value="4">Item4
</select>
</form>

<?php
if (isset($_POST['photographerNum'])) {
   echo 'New value photographerNum::' . $_POST['photographerNum'];
}
?>  

Sign up to request clarification or add additional context in comments.

5 Comments

This works to show me the selection in the html when i choose a drop down but how do i update my php variable?
@MatrixHyperLoop I have updated the post, it displays the initial value of the variable and the new value after that it has been selected an item.
The possible problem I see with this is that this select option is already part of another form
@MatrixHyperLoop, in that case, you could use Jquey and get the select "id" and after that, to do a request with the "photographerNum" value.
can you show me what the code to do that would look like? i'm pretty new to jquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.