php textfield output to variable

Discussion in 'PHP' started by shadow007, May 8, 2010.

  1. #1
    Hi

    Can anyone help me, I am trying to create a textfield with a button so when a user enters a value into that field and clicks the button the value is stored in a variable on the same page.

    Any help appreciated

    Thanks
     
    shadow007, May 8, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How exactly are you wanting the value stored on the page? In a hidden field, in a javascript variable, through sessions?
     
    JAY6390, May 8, 2010 IP
  3. shadow007

    shadow007 Peon

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I suppose a session.
     
    shadow007, May 8, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    JAY6390, May 8, 2010 IP
  5. shadow007

    shadow007 Peon

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Its the actual method of the processing script Im struggling with. Getting the variable from the form.
     
    shadow007, May 8, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Create a simple GET/POST form (if you don't know how to do this then you're going to need to lookup some tutorials)
    then use
    $_SESSION['your_session_var_name_here'] = $_GET['text_field_id_here'];
    PHP:
    use $_POST if you use the POST method for your form
     
    JAY6390, May 8, 2010 IP
  7. live.co.uk

    live.co.uk Banned

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i prefer to use the $_post
     
    live.co.uk, May 8, 2010 IP
  8. lorkan

    lorkan Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Make sure you use the $_POST function and not the $_GET function since you are using a textfield, where you probably will have lots of characters. $_GET has a limit of characters, and they are seen in the url on top of the page, which is not so nice - therefore, use $_POST in this case!

    Good luck!
     
    lorkan, May 8, 2010 IP
  9. live.co.uk

    live.co.uk Banned

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yes $_GET is not prefered in that case
     
    live.co.uk, May 8, 2010 IP
  10. shadow007

    shadow007 Peon

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    So if I passed the variable from the textfield field from page 1 to page 2 how would I store that variable so if i went to say for example page 3 upon returning to page 2 my variable would still be stored?

    Thanks
     
    shadow007, May 19, 2010 IP
  11. walterbell2331

    walterbell2331 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I would store the info in a database for this or you could write another session for this.
     
    walterbell2331, May 19, 2010 IP
  12. mike.judd

    mike.judd Greenhorn

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #12
    Don't use the session variable because that will piles up into client's memory. When you want to retrieve value from a from, do like this

    Put this HTML code in your site

    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="txtField"/>
    <br/>
    <input type="submit" name="submit" value="Click here to submit"/>
    </form>

    Put this PHP code in the head of the site

    <?php
    if (isset($_POST['txtField']) && !empty($_POST['txtField']) {
    //Do sth here
    }
    ?>

    That's how you want to retrieve the value when a user click on a button and stay in the same page

    Now, if you want the value to be passed to page 2, in the action property of HTML form, replace it with the URL of page 3

    If you want a value to be consistent through page 1, 2, and 3, one way is stored in session and another way is stored in the database. Normally, you will create a class with a distinct ID and then on page 1 you save your variable into your class then in page 2 and 3 just load this class and retrieve the value.
     
    mike.judd, May 19, 2010 IP
  13. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #13
    in page1 store your session variable after you submit and you can then call it in page 2 and 3. make sure all your pages have session_start(); at the beginning
     
    gapz101, May 20, 2010 IP
  14. shadow007

    shadow007 Peon

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I tried this but Im getting a errors, can you see what it is?

    Thanks

     
    shadow007, May 20, 2010 IP
  15. nine72

    nine72 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    after reading through this I am doing mostly the same thing.
    I have a form page that posts to another where I am converting the "POST" data to session varriables so I can use it in the creation of an XML string.

    I convert the post data using the following:

    
           function StripSpecChar($val) {
    	return (preg_replace('/[^a-zA-Z0-9" ".@\:\/_]/','', $val));
    	}
    	
    	//Convert each POST data field from the form to PHP Session state
    	foreach ($_POST as $key => $val) { 
           $_SESSION[$key] = StripSpecChar($val);  
    	}
          
    PHP:
    So what this is doing is
    1) taking out the special characters that I don't want in my db.
    2) converting anything that came over as a "POST" value in to a $_SESSION var.

    Exp:

    <form method="post" name="form1" id="form1" action="my_process.php"> 
    <input name="phone" type="text" value="">
    <input type="submit" name="submit">
    </form>
    HTML:
    and phone="555-55-5555"

    when it hits my process page the first function strips the "-" from the number
    the next function takes the "POST" phone and value and converts to
    $_SESSION['phone']=555555555;

    and I can call it where I need to use it by doing:

    print $_SESSION['phone'];

    Dont know if this is what you were after but just trying to help.
     
    nine72, May 20, 2010 IP
  16. shadow007

    shadow007 Peon

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    This was the code I was looking for, thanks alot. :)
     
    shadow007, May 21, 2010 IP