0

I have 2 comboboxes and i want to pass the selected value to anoither php by ajax the problem is that how can pass the tow vaules in one function

    <script>
function showUser(str,str2) {
    if (str == "" ||str2 =="" ) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","cg_comboBox.php?q="+str+"&p="+str2,true);
        xmlhttp.send();
    }
}
</script>

<script>
function fun(v)
{
        document.getElementById("xxx").innerHTML = v;       
}

</script>

html

  <div class="form-group">
        <label for="GroupName" class="col-sm-2 control-label">Movie Name</label>
    <div class="col-md-2">
    <select class="combobox" width="200" style="width: 200px" name="mov" id="mov" onchange="fun(this.value)">
    <option value="">Select Movie </option>
      <?php //code  ?>

    </select>
            <br>
</div>



<label for="GroupName" class="col-sm-2 control-label">Group Name</label>
<div class="col-md-2">
        <?php $x = "<div id=\"xxx\" name=\"xxx\" ><b></b></div>";
        //echo $x;  ?>

    <select class="combobox" width="200" style="width: 200px" onchange="showUser(this.value,x)" id='x'>
        <option value="">Select Cinema Group</option>
      <?php //code ?>

    </select>

i want to add $x value in the second parameter but how?

0

2 Answers 2

0

This should solve your problem:

onchange="showUser(this.value,'"+<?php echo $x; ?>+"')"
Sign up to request clarification or add additional context in comments.

2 Comments

I have made some change, try it.
That should be onchange="showUser(this.value,'<?php echo $x; ?>')"
0

Your problem is most likely that you are naming your script with an HTML or HTM extension. Name your script file with a PHP extension and not an HTML extension. This is a common problem.

Once you do that, the solutions posted by others will work, for example:

<?php $x = 'something!'; ?>
onchange="showUser(this.value,'"+<?php echo $x; ?>+"')"

First assign a value fo $x and then use the value in javascript.

If your .js file is static, and not output by PHP, then have your PHP output a variable dynamic definition, as follows:

var myvar = <?php echo $x; ?>;

And in your HTML page use the variable as follows:

onchange="showUser(this.value, myvar);"

That should do it! :)

3 Comments

my extension is php, the problem is how can i assign value to $x, becuase i have onchange="fun(this.value) to send the value of the first combox. so the problem is here: <?php $x = "<div id=\"xxx\" name=\"xxx\" ><b></b></div>"; //echo $x; ?>
When you output the code with PHP, you have the absolute power over the Javascript code it outputs. try what my edit says.
Wait a minute, i think i get what your problem is, your javascript code is in a different file than the PHP code? In that case, what you need is to define a variable in your main module! and use that variable in your secondary module! Wait a minute i will edit the code...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.