AJAX + jQuery variables do not pass to PHP

后端 未结 4 867
死守一世寂寞
死守一世寂寞 2021-01-20 20:08

I am trying to do something very simple: Pass 2 text variables to a php script and insert them into a MySQL db. For some reason however I can\'t get the variables to pass (s

相关标签:
4条回答
  • 2021-01-20 20:50

    The "data" parameters are sent as POST varaiables rather than GET variables. Try $_POST in the PHP

    0 讨论(0)
  • 2021-01-20 20:53

    running on a mobile phone

    Well I don't know if my script above will do in a mobile phone, it needs Javascript enabled!

    0 讨论(0)
  • 2021-01-20 20:55

    TEST THIS IN a test.php file (name matters):

    <?PHP 
    if(isset($_POST['user_name']))
    { 
    $post_output=
    'Hello '.strtoupper($_POST['user_name']).' from '.strtoupper($_POST['user_city']).'!
    
    This is an other random:'.rand(23,46).'.
    
    The previous random is still alive!
    
    I guess you can insert these 2 values 
    in the database on your own now, don\'t you?!';
    
    
    $get_output=
    '
    
    
    ___________________________________________
    Well if you insist you can keep using get on parallel
    This is what $_GET says:
    
    '.strtoupper($_GET['getMessage']).'
    
    And finally you can avoid post at all, to do that:
    1.Use get instead of post inside the insertToDB function
    2.Use send(null) instead of send(params)
    3.Don\'t send the headers
    
    HOWEVER I LIKE POST!';
    echo $post_output;
    print $get_output;
    exit;
    }
    ?>
    
    <html>
    
    <head>
    
    <script language="javascript" type="text/javascript"  >
    <!--
    
    var request = false;
    try { 
      request = new XMLHttpRequest(); 
    } catch (trymicrosoft) {                         
      try { 
        request = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (othermicrosoft) {
        try {
          request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {                  
          request = false;       
        }
      }
    }
    
    if (!request) 
      alert("Error initializing XMLHttpRequest!"); 
    
       function insertToDB() 
       { 
            var url = "test.php?getMessage=Hi%20There!%20Use%20me%20if%20you%20like..";
            var params = "user_name=" +  (document.getElementById("user_name").value)+ 
            "&user_city="+(document.getElementById("user_city").value);
    
            request.open("POST", url, true); 
    
            request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
            request.setRequestHeader("Content-length", params.length);
            request.setRequestHeader("Connection", "close");
    
            request.onreadystatechange = updatePage;
            request.send(params);
    
       }////////////////////
    
       //You're looking for a status code of 200 which simply means okay.
       function updatePage() {
         if (request.readyState == 4) {
           if (request.status == 200) 
           { 
           //alert(decodeURIComponent(request.responseText));
    
                 alert(request.responseText);
           } 
           else{
             //alert("status is " + request.status);
             }
         }
       }
    
    
    // -->
    </script>
    </head>
    
    <html>
    <body>
    
    This is a random:<?PHP echo rand(1,23);?><br>
    
    
    <input type="text" name="name" value="your name here"  size=80 id='user_name'   ><br>
    <input type="text" name="city" value="your city goes here"  size=80  id='user_city'  ><br>
    <input type="submit" name="bttn" value="Go" onClick="insertToDB( );" > 
    
    
    
    
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-20 21:02

    This has to work:

    <script type="text/javascript">
    $(document).ready(function() {
       //you can wrap the code into an event, e.g click()
       var colour=...
       var size=...
       $.post("http://www.website.com/bubblingajax.php", { colour: colour, size: size },
       function(data) {
         alert("Respond: " + data);
       }); 
    });
    
    </script>
    

    and the PHP (only changed get to post)

    <?php
        try
        {
            $connection = mysql_connect("#");
            mysql_select_db("#");
    
            $colour = mysql_real_escape_string($_POST['colour']);
            $size = mysql_real_escape_string($_POST['size']);
    
            mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')");
            mysql_close($connection);
            echo "SUCCESS";
            echo $colour;
            echo $size;
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
    ?>
    

    Also to debug, I would suggest using firebug or chrome's build in inspect tools.

    0 讨论(0)
提交回复
热议问题