1

I'm attempting to make a selection and pass the variable to a .php page. I can do it with the jQuery examples on a date selector ... but can't figure it out on the menu selector. When I use this control in conjunction with the datepicker, it allows me to pass both variables, but when I just use it by itself, it doesn't hand it off.

<meta name="viewport" content="width=device-width">
  <meta charset="utf-8">
  <title>DailyRecords</title>
  <link rel="stylesheet" href="jquery/jquery-ui.css">
  <script src="jquery/js/jquery-1.10.2.js"></script>
    <script src="jquery/jquery-ui.js"></script>

  <html>
  <head>
  <html> 
    <head> 

        <script>
        function showUser(str) {
            if (str == "") {
          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","roster.php?q="+str,true);
    xmlhttp.send();
}
  }
  </script>

  </head>

  <body>
  <script>
  $(function() {
$( "#restaurant" ).selectmenu({

});
    });
</script>

  <table>
  <form>
  <td>
  <select name="restaurant" id="restaurant" onchange="showUser(this.value)">
    <option value="">selection</optoin>
    <option value="101">DA</option>
    <option value="102">FV</option>
    <option value="103">CS</option>
    <option value="104">TO</option>
    </select>
  </td>  
  </form>

  </table>

  <div id="txtHint"><b>Select restaurant</b></div>


  </body>
  </html>

3 Answers 3

3

Use my code it will work. But make sure your jquery files jquery/js/jquery-1.10.2.js available in the right path or not?

<html>
<head> 
<meta name="viewport" content="width=device-width">
<meta charset="utf-8">
<title>DailyRecords</title>
<link rel="stylesheet" href="jquery/jquery-ui.css">
<script src="jquery/js/jquery-1.10.2.js"></script>
<script src="jquery/jquery-ui.js"></script>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
}
else
{
$.ajax({

url: "roster.php",
data : "q="+str, //if you want to pass one variable 
//data : "name="+name+"&natives="+natives, //if you want to pass more than 1 variable
type : "POST", // if you want to pass get method put "GET"
success :function(text){
alert(text);
document.getElementById('txtHint').innerHTML=text;
}
});
}
}
</script>
</head>
<body>   
<table>
<form>
<td>
<select name="restaurant" id="restaurant" onchange="showUser(this.value)">
<option value="">selection</optoin>
<option value="101">DA</option>
<option value="102">FV</option>
<option value="103">CS</option>
<option value="104">TO</option>
</select>
</td>  
</form>
</table>
<b>Select restaurant</b> <div id="txtHint"></div>
</body>
</html>

roster.php file

<?php
echo $_POST['q'];
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I even figured out how to turn off the alert myself :) Really appreciate it.
Only question I have ... my jquery style is now gone. The one I was using was really what I was looking for ... I believe it's called by the "selectmenu" ... that would keep it looking like a drop down instead of using the ios native control. any idea how I get that back in? ... I believe this was it: $(function() { $( "#restaurant" ).selectmenu({ }); });
0

since you using jquery you can do it like bellow :

<script>
function showUser(str) 
{
 var datastring ;
 datastring = "q="+str;
 $.post("roster.php",datastring,function(){
     //your result here
 });
}
 </script>

3 Comments

i'm a complete n00b on this stuff ... sorry for having to ask, but where do I put that in my code?
just replace your showUser function with my answer and put it in <script></script> tag...
just tried that with no affect. the old script, when I used it with the DatePicker it would give me the result at the bottom of the page. I was looking for the same thing to happen here ... maybe I didn't describe it well. Sorry, I'm a complete novice.
0

Use this following code

<html>
<head> 
<meta name="viewport" content="width=device-width">
<meta charset="utf-8">
<title>DailyRecords</title>
<link rel="stylesheet" href="jquery/jquery-ui.css">
<script src="jquery/js/jquery-1.10.2.js"></script>
<script src="jquery/jquery-ui.js"></script>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
}
else
{
  $.ajax({
     url: "roster.php",
     data: {"q":str}, //if you want to pass one variable 
     type : "POST", // if you want to pass get method put "GET"
     success :function(data){
            console.log(data);
            document.getElementById('txtHint').innerHTML=data;
      }
  });
}
}
</script>
</head>
<body>   
<table>
<form>
<td>
<select name="restaurant" id="restaurant" onchange="showUser(this.value)">
<option value="">selection</optoin>
<option value="101">DA</option>
<option value="102">FV</option>
<option value="103">CS</option>
<option value="104">TO</option>
</select>
</td>  
</form>
</table>
<b>Select restaurant</b> <div id="txtHint"></div>
</body>
</html>

get post value in poster.php

<?php
 if(isset($_POST['q'])){
     echo $_POST['q'];
 }
?>

Comments

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.