Having trouble using "implode"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
redrocket1855
Forum Newbie
Posts: 8
Joined: Tue Sep 15, 2009 12:30 am

Having trouble using "implode"

Post by redrocket1855 »

My problem is I have multiple checkboxes and I would like to display the contents of the checkboxes when the form is submitted not the word Array....... the part of the code I am having trouble with is

Code: Select all

<b>Topics:</b> 
 
<?php
            // output checked attribute for the topic checkboxes
            function topicChecked($v){
                global $missing;
                if (isset($_POST['topic'])&&(!empty($missing))&&in_array($v,$_POST['topic'])){
                    $checked = "checked";
                } 
                else {
                    $checked = "";
                }
                     [color=#FF0000] //My problem is below with the "implode" from what I am told      [/color]
                $array = array('Cooking', 'Gardening', 'Codeing');
                $comma_separated = implode(",", $array);
 
                    echo $comma_separated;
 
                //The below echo code displays the word "array"
                //echo $checked;
 
}
?>
        <input type="checkbox" name="topic[]" value="Cooking" <?php topicChecked('Cooking')?>> Cooking 
        <input type="checkbox" name="topic[]" value="Gardening" <?php topicChecked('Gardening')?>> Gardening 
        <input type="checkbox" name="topic[]" value="Coding" <?php topicChecked('Coding')?>> Coding<br>
 
Last edited by redrocket1855 on Wed Sep 30, 2009 2:36 am, edited 1 time in total.
redrocket1855
Forum Newbie
Posts: 8
Joined: Tue Sep 15, 2009 12:30 am

Re: Having trouble using "implode"

Post by redrocket1855 »

This is the full code if it will help... When you fill out the form and check the "topic field" the display does not read 'Cooking', 'Gardening',or 'Codeing' (depending on user input) only the word Array:

Form Processing Exercise
Dear user,
thank you for your interest. You have submitted the following information:
name: Sean
email: Sean@hotmail.com
topic: Array
frequency: daily
Degree: English
Comments: TEXT FIELD
register: Comfirm

Code: Select all

<?php
    
    if (array_key_exists('register',$_POST)){
        
        $required = array("Name", "email", "frequency");
        $missing = array();
 
        
        foreach ($_POST as $key => $value) {
            $temp = trim($value);
            if (empty($temp) && in_array($key,$required)){
                array_push ($missing, $key);
            }
        }
        //print_r ($missing); // this line was for the debugging purpose, allows you to see what's inside the missing array.
        
        
        if (empty($missing)){
         
            $output = "<p>Dear user,<br>thank you for your interest.  You have submitted the following information:<br><br>";
            foreach($_POST as $key=>$value){
                $output .= "<b>$key</b>: $value <br>";
            }
        } else {
 
            $output = "Dear user,<br>The following required fields are missing in your registration.  Please check your form again and fill them out.  <br>Thank you.<br>\n<ul>";
            foreach($missing as $m){
                $output .= "<li>$m";
            }
            $output .= "</ul>";
        }
    }
    
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Form Processing Exercise</title>
</head>
 
<body>
<div id="header">
    <h3> Form Processing Exercise </h3>
    </div>
<div id="registrationForm">
 <!-- Message to the user-->
<?php echo $output; ?>
 <!-- End of message -->
 <hr>
 <b>Free Newsletter Registration</b>
  <form action="" method="post">
    Name: <input type="text" name="name" value="<?php
        if(!empty($missing)){
        echo htmlentities($_POST['Name']);
        }
        ?>">
        <br>
 
    Email: <input type="text" name="email" value="<?php
        if(!empty($missing)){
        echo htmlentities($_POST['email']);
        }
?>">
 <br>
 <b>Topics:</b> 
 
<?php
            // output checked attribute for the topic checkboxes
            function topicChecked($v){
                global $missing;
                if (isset($_POST['topic'])&&(!empty($missing))&&in_array($v,$_POST['topic'])){
                    $checked = "checked";
                } 
                else {
                    $checked = "";
                }
                $array = array('Cooking', 'Gardening', 'Codeing');
                $comma_separated = implode(",", $array);
 
                    echo $comma_separated;
                
                //echo $checked;
                
 
            }
?>
        <input type="checkbox" name="topic[]" value="Cooking" <?php topicChecked('Cooking')?>> Cooking 
        <input type="checkbox" name="topic[]" value="Gardening" <?php topicChecked('Gardening')?>> Gardening 
        <input type="checkbox" name="topic[]" value="Coding" <?php topicChecked('Coding')?>> Coding<br>
 
 
<br>
 <b>Frequency:</b>  
 <br />       
 
    <?php            
    function checkFrequency($value){
    if (isset($_POST['frequency'])){
    if ($_POST['frequency'] == $value){
        echo "checked";
        }
    }
}
?>
            <input type="radio" name="frequency" value="daily"<?php
            checkFrequency('Daily')?>> Daily
            <input type="radio" name="frequency" value="weekly"<?php
            checkFrequency('Weekly')?>> Weekly
            <input type="radio" name="frequency" value="monthly"<?php
            checkFrequency('Monthly')?>> Monthly
<br />
    <select name="Degree">
            <option>English</option>
            <option>Spanish</option>
            <option>French</option>
</select>
<br />
    Comments
<br />
        <textarea name="Comments"></textarea>
    <input type="Submit" name="register" value="Comfirm">
  </form>
  
</div>
</body>
</html>
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: Having trouble using "implode"

Post by peterjwest »

That code is fine, I've tested it. What version of PHP are you using?

Code: Select all

$array = array('Cooking', 'Gardening', 'Coding');
echo implode(", ", $array);
//Output: Cooking, Gardening, Coding
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Having trouble using "implode"

Post by califdon »

You can't simply echo or print the values in an array. There's a special PHP function print_r() for doing that.
redrocket1855
Forum Newbie
Posts: 8
Joined: Tue Sep 15, 2009 12:30 am

Re: Having trouble using "implode"

Post by redrocket1855 »

The code does work.... BUT when you submit the form on the confirm page under "topic" it is not the topic with the check buttons but the work array.... i tried print_r and still got the word array under topic

Code: Select all

function topicChecked($v){
                global $missing;
                if (isset($_POST['topic'])&&(!empty($missing))&&in_array($v,$_POST['topic'])){
                    $checked = "checked";
                } 
                else {
                    $checked = "";
                }
                    [color=#FF0040]print_r( $checked);[/color]
                
 
            }
?>
 
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: Having trouble using "implode"

Post by peterjwest »

There is nothing in your code that should print "Array", there must be something in your code which says (approximately):

Code: Select all

$variable = array();
echo $variable;
I did notice here that you are using "topic[]":

Code: Select all

<input type="checkbox" name="topic[]" value="Cooking" <?php topicChecked('Cooking')?>> Cooking
<input type="checkbox" name="topic[]" value="Gardening" <?php topicChecked('Gardening')?>> Gardening
<input type="checkbox" name="topic[]" value="Coding" <?php topicChecked('Coding')?>> Coding<br>
I don't know what you think this is doing but it's not adding a value to the array. It's adding:

Code: Select all

$_POST['topic[]'] = "Cooking";
$_POST['topic[]'] = "Gardening";
$_POST['topic[]'] = "Coding";
Each of which overwrites the last, so you end up with:

Code: Select all

$_POST['topic[]'] == "Coding";
Post Reply