Latest PSU headlines:

Results 1 to 8 of 8
  1. #1
    Master Poster
    ex nihilo7's Avatar
    Join Date
    Nov 2006
    Age
    25
    Posts
    3,360
    Rep Power
    67
    Points
    1,998 (3,000 Banked)

    I need help with javascript/php!

    Hi guys, I'm trying to figure out how to parse results from one form into another form. I never learned php or javascript while in school, so I'm trying to learn it now.

    Specifically, what I am trying to do right now is display a list of checkboxes via html5 for the user to select, and then based on what the user selects, have that fill into another form via a string. I don't know if I should do this using php or if I could do it just using javascript. I'm not really trying to upload to a database as far as I know, but that is something that I should learn as well.

    So for checkbox:

    Apple = checked
    Orange
    Mango = checked


    New Form
    Fruit Selected:
    Apple
    Mango


    My html code:


    Here is a picture of what it looks like:


    Again, I want the value of what is actually checked to display in a new page/form.


    Thanks and sorry if I am not clear!
    PS4|PS3|PS2|PS1|PSVITA|XBOX 360|XBOX|N64|3DS XL|DSi|Gameboy Advance|Gameboy Color|PC

  2. #2
    Forum Guru
    [DT]'s Avatar
    Join Date
    May 2008
    Location
    Vilano Beach, FL
    Posts
    3,867
    Rep Power
    70
    Points
    17,992 (0 Banked)
    You mentioned PHP, so I’m assuming you’re on a server running PHP and your dev environment is ready to go!

    Basically, web apps send data between pages by the URL/Querystring which you’ll see as some_page.php?key=value (which can be done using the form method GET, or manually constructing the query string, etc.), or by a POST (like your current FORM).

    Then the “receiving” page just needs to access those values. In PHP, this is done through the $_GET[] and $_POST[] variables.

    If you POST your form, you’ll need to indicate the page that will handle the POST processing, so let’s say the page you posted is form_main.php (or .htm since it doesn’t actually contain any server script). You’ll just want to change the FORM section to something like this:



    Then you need to code up the form_handler page, here’s a few basics:

    The PHP parser looks for opening closing tags to indicate there’s PHP script.


    // your code goes here

    ?>

    (those double // are comment tags, everything behind them is ignored...)


    Then to access any FORM element, that was posted, you use the $_POST function like this, and referencing the NAME value used in the original form:


    // your code goes here

    echo $_POST[‘TextBoxFirstName'];

    ?>

    So that would’ve assumed a form field like

    The echo function writes the value after it to the screen, so you can do literals like, echo 'this is fun';

    The value you see echo’ed to the screen is whatever was entered in the form field. So you access the VALUE by referencing the NAME.

    Note lines have a terminating character of ;

    Typically you’d set the posted values into variables for more concise use (especially if you’re using them multiple times),

    $firstname = $_POST['TextBoxFirstName’];

    Then you could echo $firstname, or use it in a DB proc, etc., etc.

    OK, additional fun and excitement:

    A form field/input can contain a VALUE attribute (like you’ve done). Same thing as above, except it isn’t user supplied, so in your HTML example:

    name = "group1"
    value = "One" />Apple



    Accessing that Checkbox via POST like this, $_POST['group1’] will return the value of 'One'

    ...however, there’s a couple of catches ... you were waiting for this part, right?

    The Checkbox doesn’t “exist” in the POST unless it’s checked, so in other words, not checked, no $_POST[], checked there is a $_POST which corresponds to the set VALUE (in your case, “One”)

    No problem, there’s a function called isset[] (Is Set) that can check for the existence of a value (returns boolean true/false), so a checkbox handler would be something like:


    if(isset($_POST[‘CheckBoxApple']))
    {
    echo “Selected apple.";
    }
    else
    {
    echo “Apple not selected.";
    }
    ?>

    Note that I really didn’t deal with the VALUE as I just wanted to know if it was set, the checkbox itself provides the value logic so to speak.

    The construct above is a typical bit of control logic:

    if(check for something)
    {
    logic if it checks/exists

    }
    else
    {
    logic if it doesn't

    }


    Now, you’ve got an extra level of complexity because your checkboxes have the same NAME, that means, the code will not know which one to access, so they’ll be handled in an array, where you’ll need to recurse through, determine if it’s set, then get the value, and you’ll want to use the [] to denote the name will be handled as an array.

    In fact, I’d say maybe rename your inputs to different names to get a handle on all the above and we can revisit, or I can toss up some code that will take the array group1 and recurse it, just didn’t want to overload you

    [edit]

    OK, I’ll toss up handling a checkbox array since I’m already there

    put the [] suffix on each element name, so group1[], and give each element a name that conveys the logic (the name of the fruit would be perfect):

    name = "group1[]"
    value = "Apple" />Apple


    Then in the form_handler page:


    $fruit = $_POST['group1'];

    if(empty($fruit))
    {
    echo "Pick some fruit!";
    }
    else
    {
    $cnt = count($fruit);

    for($i=0; $i < $cnt; $i++)
    {
    echo $fruit[$i] ."

    ";
    }
    }



    ?>

    What this does is create an array of all the items selected in the same group1[], arrays are lists of data, and each element can be selected by using an indexer, which is a integer value that indicates the position in the array starting from 0

    The count() function returns the number of items (you’d have a max of 3), then the for loop gets each one by substituting a number starting from 0 to the number of items total in the array, so $fruit[0], $fruit[1]

    The . notation is to concatenate strings, and you can see I’ve put a string value of

    to add a little HTML page break for readability.

    Last edited by [DT]; 05-25-2012 at 14:33.
    "Remember, you're young only once but you can be immature forever."

  3. Likes ex nihilo7 likes this post
  4. #3
    Master Poster
    ex nihilo7's Avatar
    Join Date
    Nov 2006
    Age
    25
    Posts
    3,360
    Rep Power
    67
    Points
    1,998 (3,000 Banked)
    Dude...Sweet! Thanks! I'm going to try this right now!

    The Checkbox doesn’t “exist” in the POST unless it’s checked
    Yep, that was another thing I wasn't sure of.

    edit: I got the non-array examples to work! It took me about 10 minutes to realize I was trying to run the form outside of my localhost server. I was like "why is echo not working!"

    I'm going to try the array next.

    edit2: I got the array working correctly! The only error is that when I don't select anything in the array, it gives me an error saying "undefined index". It must not know how to handle 0 selections.

    here is a screenshot of it:

    Last edited by ex nihilo7; 05-25-2012 at 22:38.
    PS4|PS3|PS2|PS1|PSVITA|XBOX 360|XBOX|N64|3DS XL|DSi|Gameboy Advance|Gameboy Color|PC

  5. #4
    Master Poster
    ex nihilo7's Avatar
    Join Date
    Nov 2006
    Age
    25
    Posts
    3,360
    Rep Power
    67
    Points
    1,998 (3,000 Banked)
    [DT] or anyone else, do you know how to strip the html from a php array on the fly for use when sending an html email?

    PHP Code:
    $N count($ends); 
        echo (
    '<table border="1" class="lines" width="100%">');
        echo (
    "<tr><th>Look For</th>");
        echo (
    "<th>Observed Comments</th></tr>");
        for(
    $i=0$i $N$i++)
        {
          echo(
    "<tr><td width='50%'>");
          echo(
    $ends[$i] . "<p>");
          echo(
    "</td><td width='50%'>");
          echo (
    " ");
          echo(
    "</td></tr>");
        }
        echo (
    '<table>');
        echo (
    "<p>");
      } 
    I need to basically strip out the html somehow and email it at the same time.
    PS4|PS3|PS2|PS1|PSVITA|XBOX 360|XBOX|N64|3DS XL|DSi|Gameboy Advance|Gameboy Color|PC

  6. #5
    Forum Guru
    [DT]'s Avatar
    Join Date
    May 2008
    Location
    Vilano Beach, FL
    Posts
    3,867
    Rep Power
    70
    Points
    17,992 (0 Banked)
    Yep, there’s a function built right into PHP:

    strip_tags($string)

    returns a string with the tags removed ($string is the input string).

    So like:

    $str = '

    this is some markup

    ';

    echo strip_tags($str);

    Results in:

    this is some markup


    "Remember, you're young only once but you can be immature forever."

  7. #6
    Master Poster
    ex nihilo7's Avatar
    Join Date
    Nov 2006
    Age
    25
    Posts
    3,360
    Rep Power
    67
    Points
    1,998 (3,000 Banked)
    Quote Originally Posted by [DT] View Post
    Yep, there’s a function built right into PHP:

    strip_tags($string)

    returns a string with the tags removed ($string is the input string).

    So like:

    $str = '

    this is some markup

    ';

    echo strip_tags($str);

    Results in:

    this is some markup


    Thanks for the reply! I can't seem to get it to work, but I tried it in a hurry (I have softball game in a short while).

    I tried this:
    $message = join($_POST['group1']).join($_POST['group2']).join($_POST['group3']);

    and it prints out the correct strings from my arrays in this fashion:

    Code:
    string, string, string,etc.
    but I'm not sure if I can apply the correct html formatting I want thats present in my array:

    Code:
    string
    string
    string
    
    string 
    string
    string
    edit: I'm trying to send an html email generated by my survey form
    PS4|PS3|PS2|PS1|PSVITA|XBOX 360|XBOX|N64|3DS XL|DSi|Gameboy Advance|Gameboy Color|PC

  8. #7
    Master Poster
    ex nihilo7's Avatar
    Join Date
    Nov 2006
    Age
    25
    Posts
    3,360
    Rep Power
    67
    Points
    1,998 (3,000 Banked)
    Quote Originally Posted by ex nihilo7 View Post
    Thanks for the reply! I can't seem to get it to work, but I tried it in a hurry (I have softball game in a short while).

    I tried this:
    $message = join($_POST['group1']).join($_POST['group2']).join($_POST['group3']);

    and it prints out the correct strings from my arrays in this fashion:

    Code:
    string, string, string,etc.
    but I'm not sure if I can apply the correct html formatting I want thats present in my array:

    Code:
    string
    string
    string
    
    string 
    string
    string
    edit: I'm trying to send an html email generated by my survey form
    So I tried it again tonight and I got it working pretty well now I think. Thanks [DT]!
    PS4|PS3|PS2|PS1|PSVITA|XBOX 360|XBOX|N64|3DS XL|DSi|Gameboy Advance|Gameboy Color|PC

  9. #8
    Forum Guru
    [DT]'s Avatar
    Join Date
    May 2008
    Location
    Vilano Beach, FL
    Posts
    3,867
    Rep Power
    70
    Points
    17,992 (0 Banked)
    OK, cool, I was going to mention that the underlying tag parser could/can occasionally get frazzled if the markup isn’t to spec.
    "Remember, you're young only once but you can be immature forever."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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

PSU

Playstation Universe

Reproduction in whole or in part in any form or medium without express written permission of Abstract Holdings International Ltd. prohibited.
Use of this site is governed by our Terms of Use and Privacy Policy.

vBCredits II Deluxe v2.1.0 (Pro) - vBulletin Mods & Addons Copyright © 2010-2014 DragonByte Technologies Ltd.