1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development PHP Help

Discussion in 'Software' started by :: Phat ::, 25 May 2006.

  1. :: Phat ::

    :: Phat :: Oooh shakalaka!

    Joined:
    7 Jun 2002
    Posts:
    4,886
    Likes Received:
    3
    I would like to have a simple single text box on the wepage which asks users to enter some data.

    For example, a user has just typed DATA into the box.
    When they click submit a new window (_blank) will open, with a web address I.E.

    http://www.thisaddress.co.uk/etc?DATA

    I would also be nice if DATA was logged in a MySQL database along with say a Unique ID and timestamp?

    Any suggestions?
     
  2. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    Well, the way I would do it, is like so:
    form.html (or .php, but you don't need php/scripting to send data, just to process it)
    Code:
    <form method="GET" action="/etc/" target="_blank">
    <input type="text" name="var" />
    <input type="submit" value="Submit" />
    </form>
    
    Then, let's assume this form is anywhere on http://www.thisaddress.co.uk/
    If I typed in "DATA", then click Submit, a new window will open with the address:
    http://www.thisaddress.co.uk/etc?var=DATA

    Inside your PHP script (possibly /etc/index.php):
    PHP:
    <?php
    //if register_globals is on, you do not need to do this, although it is much easier to write more secure scripts with register_globals off
    if(isset($_GET["var"])) {
      
    $var $_GET["var"];
      
    //do whatever you want with VAR here, such as look it up in a database, or store it in a database
     //as an example:
      
    $dbc = @mysql_connect("localhost""username""password") or die("could not connect to database");
      
    $query "INSERT INTO tb_vars (IP, timestamp, var) VALUES ('" $_SERVER['REMOTE_ADDR'] . "', " time() . ", '" mysql_escape_string($var) . "');";
      if(@
    mysql_query($query$dbc)) {
        
    //do something like include a file, or just tell the user it worked
        
    echo("Successfully stored your variable, timestamp, IP, and credit card number.  Thanks!");
      } else {
        
    //do something like include a file, or just tell the user it DIDN'T work
       
    echo("Ahh crap, something messed up on the database... I guess I should pay the hosting bills next time.");
      }
    } else {
      
    $var null;
      
    //do whatever needs to be done, since you don't have any variables
      
    echo("Oops, you didn't send any variables, did you get here by mistake, or just leave it blank?  Either way, please go away.");
    }
    ?>
    Does that make sense?

    Sending form data in PHP is super simple. If you used method="POST" instead of method="GET", then you could use the same script, but just access $var by $_POST["var"] instead of $_GET["var"]

    On the other hand, if you used $_REQUEST["var"], that will pull "var" from GET or POST, but it is cleaner to know where you are getting your data from.

    To get familiar with PHP processing forms, I would set up a form with all kinds of fields (checkboxes, radio buttons, drop-down menus, etc.) then just have it POST to a php script that simply does the following:
    PHP:
    <?php var_dump($_POST); ?>
    we'll call that dump.php

    Now, in the same folder as that, make an html file like this:
    Code:
    <html>
    <head><title>OneSeventeen Rocks</title></head>
    <body>
    <form method="POST" action="dump.php">
    <input type='text' name='foo' /> <br />
    <select name='bar'>
      <option value='1'>Red</option>
      <option value='2'>Green</option>
      <option value='3'>Blue</option>
    </select>
    <input type='radio' name='baz' value='sweet' /> Sweet <br />
    <input type='radio' name='baz' value='sour' /> Sour <br />
    <input type='checkbox' name='qux' value='yup' /> Yes? <br />
    <input type='submit' name='thesubmitButton' value='Submit' />
    </form>
    </body>
    </html>
    
    Then play with that form a bit.

    Once you are comfortable with that, modify the form to add these 3 lines to the form:
    First Name: <input type='text' name='name[first]' /> <br />
    Last Name: <input type='text' name='name[last]' /> <br />
    Middle Initial: <input type='text name='name[mi]' /> <br />

    Now fill that out and submit it.

    Crazy, huh?
     

Share This Page