I need help with javascript/php!

ex nihilo7

Master Poster
Nov 13, 2006
3,373
11
38
#1
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:
Code:
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Please help lol</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div id = "container">
<h1>Fruit Picking</h1>
<form method="post">

<h2>Directions: Select each of the types of fruit you want!</h2>
       <fieldset>
         <legend>Fruit</legend>
         <p>
           <label>Please select up to three</label><br />
             <input type = "checkbox"
                 name = "group1"
                 value = "One" />Apple<br />
           
             <input type = "checkbox"
                 name = "group1"
                 value = "Two" />Orange<br />
             
                  
             <input type = "checkbox"
                 name = "group1"
                 value = "Three" />Mango<br />
          
         </p>
       </fieldset>
       
      <input type="submit" value="Create It" />
</form>

<h1> Thank You for using this form! </h1>
</div>

</body>
</html>

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!
 

[DT]

Forum Guru
May 1, 2008
3,851
64
0
Vilano Beach, FL
#2
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:

<form method="post" action=“form_handler.php”>

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.

<?PHP

// 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:

<?PHP

// your code goes here

echo $_POST[‘TextBoxFirstName'];

?>

So that would’ve assumed a form field like <input type=text name=TextBoxFirstName>

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:

<input type = "checkbox"
name = "group1"
value = "One" />Apple<br />


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? :D

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:


<?php
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 :D

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):

<input type = "checkbox"
name = "group1[]"
value = "Apple" />Apple<br />

Then in the form_handler page:

<?php

$fruit = $_POST['group1'];

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

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



?>

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 <p> to add a little HTML page break for readability.
 
Last edited:

ex nihilo7

Master Poster
Nov 13, 2006
3,373
11
38
#3
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!" :lol:

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:





code starting from line 31:
Code:
$fruit = $_POST['group2'];
  if(empty($fruit))
  {
    echo("You didn't select group 2 fruit.");
  }
  else
  {
    $N = count($fruit);
    echo("You selected $N door(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($fruit[$i] . " ");
    }
  }
 
Last edited:

ex nihilo7

Master Poster
Nov 13, 2006
3,373
11
38
#4
[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:
$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.
 

[DT]

Forum Guru
May 1, 2008
3,851
64
0
Vilano Beach, FL
#5
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 = '<h1>this is some markup</h1>';

echo strip_tags($str);

Results in:

this is some markup


:)
 

ex nihilo7

Master Poster
Nov 13, 2006
3,373
11
38
#6
[DT];5852078 said:
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 = '<h1>this is some markup</h1>';

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
 

ex nihilo7

Master Poster
Nov 13, 2006
3,373
11
38
#7
[QUOTE="ex nihilo7, post: 5852091]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[/QUOTE]

So I tried it again tonight and I got it working pretty well now I think. Thanks [DT]!