SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session question

    HI everybody,
    I have a huge number of input variables (strings in text fields, radio buttons...) on an input form. The users will fill some questionnarie and send it to whatever@whatever.com
    How can I retrieve all these variables on the next page "send_the_data.php", to print da questionnarie onto a *.txt file and send it to the mentioned address? I have the questionnarie.php in 4 languages.

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could iterate through the $_POST collection, filtering out any that you don't want:
    Code:
    foreach ($_POST as $key=>$value) {
      echo $key . ': ' . $value . '<br />';
    }

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that's working fine, but...
    I'm storing the labels of the quesetionnarie in variables :
    PHP Code:
    $center_drive "center drive";
        
    $shaft "Shaft diameter";
        
    $shaft_s "square bore";
        
    $shaft_r "round bore";
        
    $speed "Conveyor speed"
    On my send_the_data.php page where I retrieve $key=>$value, in $key stays i.e. "shaft_s", while I need there "square bore"...
    "shaft_s" has the value "square bore" if the language is "english"...
    How can I retrieve the strings ("square bore")instead of the values("shaft_s")?

  4. #4
    SitePoint Member
    Join Date
    Jul 2005
    Location
    Hagley Worcestershire Uk
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow not much then!

    One answer is to make your form call its-self over and over with <form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?status="check">

    When you first enter your form your status isn’t set so the script displays the empty form.

    The User completes the form and when sent the status is now set to TRUE so the form checks it’s self, and a new variable can be set $err_msg=’’; this will be the gatekeeper for checks that the form is ok.

    Are the values in the form correct? In addition you can Title Case, check valid ‘phone numbers and emails addresses etc. If there is an error, $err_msg becomes something like $err_msg=”That is not a valid email address”, and the form fields are populated with the variables from each field, $err_msg is echoed, and the User can correct their error.

    When the form is correct and $err_msg=’’; you write the form variables to session variables, $_SESSION[‘email’]=$email; etc etc.

    Sessions are difficult when you first encounter them. There are loads of “good” tutorials on sessions, all written by “experts” who leave out the obvious, so you are no wiser! Best of luck here, all I can say is that each script must start with (that’s at the beginning) session_start();

    After you write your session variables you move to your next script with something like $_SESSION['status'] = $status; session_write_close(); header ("Location: form_2.php"); exit();. Your status can be D or F or GB or indeed I, depending on the European state the User lives in.

    Next in form_2.php you read your variables back from your session $email=$_SESSION[‘email’];

    Write them to a database, email them in the sessions language, or send the variables to an excel spreadsheet, or indeed a mobile phone or PDA.

    Kill the session

    And that’s about it.

  5. #5
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh... That's a little bit complicated to me.
    If I understood, I must write ALL the form variables to session variables? The previous short script was good because it's a loop and I have over 50 variables, I won't be happy if I'll have to call session for ALL of them...

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dujmovicv
    Thanks, that's working fine, but...
    I'm storing the labels of the quesetionnarie in variables :
    PHP Code:
    $center_drive "center drive";
        
    $shaft "Shaft diameter";
        
    $shaft_s "square bore";
        
    $shaft_r "round bore";
        
    $speed "Conveyor speed"
    On my send_the_data.php page where I retrieve $key=>$value, in $key stays i.e. "shaft_s", while I need there "square bore"...
    "shaft_s" has the value "square bore" if the language is "english"...
    How can I retrieve the strings ("square bore")instead of the values("shaft_s")?
    just add one extra '$', that's all about it
    PHP Code:
    foreach ($_POST as $key=>$value) {
      echo $
    $key ': ' $value '<br />';


  7. #7
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't worked!
    The browser returned 2 times the $value, like that:
    $shaft_s : $shaft_s

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From what you've said, it should work.

    UPD: You probably have register_globals turned on. Turn it off in php.ini / .htaccess.

  9. #9
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, stereofrog, it still doesn't want to work...
    Instead, now the browser doesn't prints the $key value at all...

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... then I'm afraid you have no other choice as to post your complete code.

  11. #11
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it's too "long" to post it all, I will post a part which will discribe my probleme to you ( I hope ).
    So here are some values for the labels of my input form:
    PHP Code:
    switch ($language) {
    // case 1, 2, 4 --> the values are different i.e. $title1 = "Kundendaten"
    // if language is set to "german"
        
    case 3:
        
    $title1 "Costumer data";
        
    $company "Company";
        
    $contact "Contact person";
        
    $address "Address";
        
    $date "Date";
        
    $tel "Phone";

        
    $title2 "Product data";
        
    $use "Application";
        
    $product "Product type";
        
    $new_type "New belt type";
        
    $type "Existing belt type";
        
    $pack1 "Bulk";
        
    $pack2 "Containers";
        
    $load "Load";
        
    $diam "Diameter";
        
    $weight "Weight";
    // and so on 
    After defining all the variable values for all 4 languages there is an input form :
    PHP Code:
    <form method = "post" action = "send.php">
    <table border = "1" bordercolor="#333333" cellpadding="0" cellspacing="0" width=100%>
    <tr bgcolor = "#aaaaaa">
        <td><h2>&nbsp;1. <? print ($title1); ?></h2></td>
    </tr></table>

    <table border = "0" cellpadding="0" cellspacing="0" width=100%>
    <tr bgcolor = "#ffffff">
        <td colspan="4" width="20%"><? print ($company); ?></td>
        <td width="30%" align="right"><input type="text" name="company" size="30"></td>
        <td align="right"><? print ($date); ?></td>
        <td width="20%" align="right"><input type="text" name="date" size="20"></td>
    </tr>
    <tr bgcolor = "#ffffff">
        <td colspan="4"><? print ($contact); ?></td>
        <td align="right"><input type="text" name="cont_per" size="30"></td>
        <td align="right">Fax</td>
        <td align="right"><input type="text" name="fax" size="20"></td>
    </tr>
    <tr bgcolor = "#ffffff">
        <td colspan="4"><? print ($address); ?></td>
        <td align="right"><input type="text" name="address" size="30"></td>
        <td align="right"><? print ($tel); ?></td>
        <td align="right"><input type="text" name="phone" size="20"></td>
    </tr>
    </table>
    My problem was, that on my send.php I cuoldn't retrieve the values of the variables, but only the variable names, and of course the entered "strings" by the users. I need the string "Contact person" instead of "contact" !
    Thanks in advance

  12. #12
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    foreach($_POST as $postvar => $postval){ ${$postvar} = $postval; }

  13. #13
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks simplecode, but with your suggestion I'm still getting "only" the variable names and not the string values
    Anyway, I appreciate Your efforts

  14. #14
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think this is what you want.

    PHP Code:
    $translations = array();
    $translations[1] = array(
        
    'title1' => 'Costumer data',
        
    'contact' => 'Contact person'
    );
    $translations[2] = array(
        
    'title1' => 'Datos del cliente',
        
    'contact' => 'Persona del contacto'
    );
    // etc...
    $language 2;
    foreach (
    $_POST as $key => $value) {
        echo 
    $translations[$language][$key];
        echo 
    ": $value\n";



Bookmarks

Posting Permissions

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