0

I have this select box. The selected option is sent with ajax to a server side script.

<select id="main_select">
    <option selected="selected" value="50">50</option>
       <option value="100">100</option>
       <option value="150">150</option>
</select>

jquery script

$(document).ready(function() {
$('#main_select').change(function() {
$.ajax({
  url: "itemscript.php",
  type: "post",
  data: {
    option: $(this).val()
  },
  success: function(data) {
    $("#details").html(data);
  }
 });
 }).change();
});

And in the server sided script (itemscript.php) I get the variable from the select box like that:

if(isset($_POST["option"]))
{
    $itemlevel = $_POST["option"];
}

Now I want to extend this script to send an additional php variable called $element to the server sided script (itemscript.php). The $element variable is a part of the the current url address.

How can I send the additional $element variable within this jquery/ ajax script?

1 Answer 1

1

Just add the other data items into the data{} part, separated by commas.

Assuming that your $element data is inside an inputbox, then

HTML

<input id="element">
<br>
<select id="main_select">
    <option selected="selected" value="50">50</option>
       <option value="100">100</option>
       <option value="150">150</option>
</select>
$(document).ready(function() {

$('#main_select').change(function() {
$.ajax({
  url: "itemscript.php",
 method: "POST",
  data: {
    option: $(this).val(),
    element: document.getElementById("element").value
  },
  .success: function(data) {
    $("#details").html(data);
  }
 });
 }).change();
});

In the above, $_POST["element"] will be passed to your php scripts

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

1 Comment

Thanks, that helped me a lot.

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.