Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5

Thread: Get or Post?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    Hi all. I am trying to pass values from a form through a "url query string" to update.php. 'update.php' retrieves the string and copies it to a database. Should I be using Get or Post for this?

    I have created some code that sends the query string, but it will not post to the database. Can someone run through this simple code real quick and help me find the (probably) simple solution?

    example.html:
    Code:
    <html>
    
    <head>
    <script type="text/javascript">
    function showCustomer()
    {
    var xmlhttp;
    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;
        }
      }
            var match = document.getElementById('match').value;
        var score = document.getElementById('score').value;
        var queryString = "?match=" + match + "&score=" + score;
    
    xmlhttp.open("GET", "update.php" + queryString,true);
    xmlhttp.send();
    }
    </script>
    </head>
     
    <body>
    
    <form action="">
    <select name="match" id="match">
    <option value="Eng1 ChelseaLiverpool">Chelsea - Liverpool</option>
    <option value="EngFACup ArsenalWigan">Arsenal - Wigan</option>
    <option value="Eng2 WatBromley">Watford - Bromley</option>
    <option value="Sing1 SAFGombak">SAF - Gombak United</option>
    </select>
     
    <select name="score" id="score">
    <option value="00">0-0</option>
    <option value="10">1-0</option>
    <option value="01">0-1</option>
    <option value="11">1-1</option>
    </select>
    
    <input type="submit" onclick="showCustomer()" value="Submit Pick" />
    
    </body>
    </html>

    update.php:
    Code:
    <?php
    
    $match = $_GET['match'];
    $score = $_GET['score'];
     
    mysql_connect ('localhost', 'admin', 'password') or die ('Error: ' .mysql_error());
    mysql_select_db ("example_db");
    
    $query="INSERT INTO user (FirstName, Lastname)VALUES ('".$match."', '".$score."')";
    mysql_query($query) or die ('Error submitting');
      
    ?>

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    Your problem is the way you're trying to get the value from the select boxes.

    You want something like this:
    Code:
    var match = document.getElementById('match').options[document.getElementById('match').selectedIndex].value;
    This tells your Javascript to look at the options of your select box, find the one corresponding with the selected index, and output its value. (Keywords bolded for emphasis purposes and comprehension only...not intending to make fun of anyone here).

    Alternatively, you could also rock the simpler jQuery syntax. But you've already come quite a ways with standard Javascript so I wouldn't necessarily recommend it at this point.

    The other thing you're going to run into is that your update.php doesn't output anything. You should have it output some XML of some sort...otherwise your "txtHint" line will show absolutely nothing. How you do that, I'm not exactly sure, because I'm not a PHP guy...so I don't know how to use PHP to output XML. Someone else is going to have to jump in on that one. But the Javascript at least should be taken care of.

    The other thing I don't get is why you're writing a match and a score to a firstname and lastname field. Why not name them something like...well, match and score?
    inplaytoday likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    thanks for the help. in regards to the firstname, lastname, I just used an existing table already created. I wanted to get my head around this thing before actually building the real database.

    It seems the problem lies with PHP-Ajax-MySQL interactions, so I'll still be working on this. If I come up with a solution, it will be posted on here.

  5. #4
    Junior Member
    Join Date
    May 2011
    Location
    Austin, TX
    Posts
    6
    Member #
    27812
    Liked
    2 times
    [COLOR=rgb(20, 20, 20)][COLOR=rgb(20, 20, 20)]Here's a simple way using jQuery to POST via AJAX to PHP, which in turn will execute a Callback to the form indicating a successful submission as an alert:

    1) Add to form page header:
    Code:
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#match').val("");
        $('#score').val("");
    });
    function postPHP(){
        $.post("update.php", { 'match': $('#match').val(), 'score': $('#score').val()  },
        function(data){
            //Successful POST returns Alert
            alert(data);
        });
    }
    </script>
    2) Set up the form(doesn't require <form> tags for an AJAX call):

    <select name="match" id="match"> <option value="Eng1 ChelseaLiverpool">Chelsea - Liverpool</option> <option value="EngFACup ArsenalWigan">Arsenal - Wigan</option> <option value="Eng2 WatBromley">Watford - Bromley</option> <option value="Sing1 SAFGombak">SAF - Gombak United</option> </select> <select name="score" id="score"> <option value="00">0-0</option> <option value="10">1-0</option> <option value="01">0-1</option> <option value="11">1-1</option> </select>

    3) Modify your submit button to execute the jQuery/AJAX POST function:


    Code:
    <input type="submit" onclick="javascript:postPHP();" value="Post to PHP" />



    4) Add a callback function to your PHP to indicate whether or not the POST was successful:
    Code:
    <?php
    $match = $_POST['match'];
    $score = $_POST['score'];
    echo $match;
    echo $score;
    
    mysql_connect ('localhost', 'admin', 'password') or die ('Error: ' .mysql_error());
    mysql_select_db ("example_db");
    
    $query="INSERT INTO user (FirstName, LastName)VALUES ('".$match."', '".$score."')";
    mysql_query($query) or die ('Error submitting');
    
    //Callback to form page to indicate a successful POST
    function talkBack($callback)
    {
        $data = "AJAX-PHP POST";
        $callback($data);
    }
    function myCallback($data)
    {
        print ' was successfully passed using ' .  $data .  "\n";
    }
    talkBack('myCallback');
    ?>
    **I personally prefer to use the POST method to avoid passing any hidden or sensitive params through the QueryString. If you'd like to sent using GET, simply change the jQuery $.post(); to $.get(); on your form page and change $_POST['match']; and $_POST['score'] to $_GET['match'] and $_GET['score'] in your PHP.
    AJAX-PHP call/callbacks aren't too bad if you keep the code elegant. Hope this is able to help!

    Brandon
    [/COLOR]
    [/COLOR]
    inplaytoday and TheGAME1264 like this.

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    thanks for the help and effort, Brandon

    I am not very familiar with Jquery, but I will definitely spend some time on the info you posted to see how I can simplify my code..


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:13 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com