Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Javascript (Ajax) does not assign the proper value to $_POST["apt_time"]

    This script is written to test if the javascript assigns $_POST["apt_time"] a value of 'May120128AM' on page load and sends this key/value pair to the php script.

    The script is supposed to do just that, and the php is supposed to respond with "not available".

    The php script returns "available" to the javascript, so this means the value of 'May120128AM is not assigned to $_POST["apt_time"].

    This means the javascript doesn't work right. I can't figure out why. The lines in the javascript and php that I think are important are highlighted in red.

    The php script returns "available", which means $_POST["apt_time"] does not equal 'May120128AN'. It should equal that.

    The html and javascript:
    Code:
    <!DOCTYPE html>
    
    <html>
    <head>
    <title>My Web Page</title>
    <style type="text/css">
    #horizontal ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    }
    
    body {margin: 10em;}
    
    #form {margin: 1em 1em;l}
    
    #answer {
    padding: .2em .2em;
    margin: .5em 0 .5em 0;
    }
    
    #horizontal ul li { 
    display: inline;
    width: 200px;
    height: 50px;
    background: #ccc; 
    text-align: center;
    padding: .3em;
    }
    </style>
    
    </head>
    
    <body>
    
    <div id=horizontal>
    <form >
    <ul id="May120128AM" name="apt_time" value="May120128AM">
    
          <li name="user_id" id="May120128AM_user_id"  value="2"/></li>
    
          <li name="password" id="May120128AM_password"  value="2"/></li>
    
          <li>8:00 - 8:45 AM</li>
    
    
          <li id = 'May120128AM_Schd_Can' style=\"display: inline;
                                                    width: 200px;
                                                    height: 50px;
                                                    background: #ccc; 
                                                    text-align: center;
                                                    padding: .3em;"
          ></li>
    
          <li id = 'May120128AM_Nt_Avail' style=\"display: inline;
                                                    width: 200px;
                                                    height: 50px;
                                                    background: #ccc; 
                                                    text-align: center;
                                                    padding: .3em;"
          ></li>
    
          <li id = 'May120128AM_Avail' style=\"display: inline;
                                                    width: 200px;
                                                    height: 50px;
                                                    background: #ccc; 
                                                    text-align: center;
                                                    padding: .3em;"
          ></li>
    
    </ul>
    </form>
    </div>
    
    <script type="text/javascript"> 
      <!--
      var request;
        request = new XMLHttpRequest();
    </script>
    
    
    <script type="text/javascript">
        var apt_time = document.getElementById("May120128AM").value;
        var userid = document.getElementById("May120128AM_user_id").value;
        //var password = encodeURIComponent(document.getElementById("May120128AM_password").value);
        var parameters = "apt_time="+apt_time+"&user_id="+userid
    
        request.open("POST", "/cgi-bin/zipcode.php", true);
        request.onreadystatechange = function()
        {
           if(request.readyState == 4)
           {
              if(request.status == 200)
              {
                      var SchdCan = document.getElementById('May120128AM_Schd_Can');
                      var NtAvail = document.getElementById('May120128AM_Nt_Avail');
                      var Avail = document.getElementById('May120128AM_Avail');
    
                  if(request.responseText == "available")
                  {   
                     if(SchdCan.innerHTML == '' &&
                        NtAvail.innerHTML == '' &&
                        Avail.innerHTML == '')
                     {
                            SchdCan.innerHTL = '' 
                            NtAvail.innerHTML = '' 
                            Avail.innerHTML = "Available <button id='May120128AM_Click_To_Sch' name='apt_time' value='May120128AM' onClick='clickToSchedule90; return false'>Click Here To Schedule</button>"
                     } 
                     if(SchdCan.innerHTML == '' &&
                        NtAvail.innerHTML == 'not available' &&
                        Avail.innerHTML == '')
                     {
                            SchdCan.innerHTL = '' 
                            NtAvail.innerHTML = '' 
                            Avail.innerHTML = "Available <button id='May120128AM_Click_To_Sch' name='apt_time' value='May120128AM' onClick='clickToSchedule(); return false'>Click Here To Schedule</button>"
                     } 
                     if(SchdCan.innerHTML == '' &&
                        NtAvail.innerHTML == '' &&
                        Avail.innerHTML == "Available <button id='May120128AM_Click_To_Sch' name='apt_time' value='May120128AM'>Click Here To Schedule</button>"
                                             )
                                             
                     {
                            SchdCan.innerHTML = "" 
                            NtAvail.innerHTML = '' 
                            Avail.innerHTML = "Available <button id='May120128AM_Click_To_Sch' name='apt_time' value='May120128AM'>Click Here To Schedule</button>"
                     } 
                  }
                  if(request.responseText == "you are scheduled")
                  {
                        
                       
                                            
                                 SchdCan.innerHTML = "Scheduled <button id='May120128AM_Schd_Can' name='apt_time' value='May120128AM'>Click Here To Cancel</button>"
                      
                  }
                  if(request.responseText == "not available")
                  {
                                            
    			NtAvail.innerHTML = 'not available'
    		  
                      
    
                  }  //Closing if(responseText == "")
                  
              }  //Closing if(request.status == 200)
              
           }  //Closing if(request.readyState==4)
        }  //Closing onreadystatechange function 
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        request.send(parameters);
    
    </script>
    
    
    </body>
    
    
    </html>
    The php:
    Code:
    <?php
    if(isset($_POST["apt_time"], $_POST["user_id"]))
    {
             if($_POST["apt_time"] == 'May120128AM')
             {
                       echo "not available";
             }
             else
             {
                       echo "available";
             }
              
    }
    ?>
    Last edited by milesdriven; 05-20-2012 at 06:50 PM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    It might be because the HTML is invalid - The value attribute is invalid on the <ul> tag.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    That's it. I put the value in a hidden <input> tag, which I then put inside a new <li> tag.

    It works now. Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •