开发者

how to submit combo box value with out form using javascript?

开发者 https://www.devze.com 2023-04-07 02:04 出处:网络
I need to submit combo box value with out form. When I click on any value of combo that suppose to submit automatically using javascript. I am using PHP in backend.

I need to submit combo box value with out form. When I click on any value of combo that suppose to submit automatically using javascript. I am using PHP in backend.

<select name="select" id="select" style="width:50px;">
  <option value="25">25</option>
  <option value="50">50</option&g开发者_开发技巧t;
  <option value="100">100</option>
  <option value="All">All</option>
</select>


Here a js part:

function getinfo() {
  var aList = document.getElementById("select");
  var val = aList.options[aList.selectedIndex].value;

  // document.write("<p>Here what you select: "+val+"</p>");
  // here you can send `val` to the server using... are you using any js library?
}

And you need to change your select declaration:

<select name="select" id="select" style="width:50px;" onchange="getinfo()">
                                                      --------------------
</select>

OK, how to send val to the server is another question... If you are using jQuery - you can use jQuery.ajax(...) or any helper like jQuery.get(...). If you are vanilla-js user you can use XMLHttpRequest way and if you use any other lib - just check this lib's documentation to get helped about sending data to the server.


You can use the methods below.

1) Use Session or Cookie to send external data

2) Send a POST request using XMLHttpRequest in javascript.Checkout the link here.

3)You can use cURL function to send HTTP POST request. Please go through the link here.


Some comments:

  • The select element should always be inside a form. But you don't need to expose the form on the page--you do not need to include a submit input element.

  • You can use Javascript (JS) to submit the form after the user has changed the value of the select.

  • Or you can use JS with Ajax to submit the form asynchronously in the background--the user will not be aware that the data had been submitted and the page will not reload or "flash"

What would you like to do?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号