JohnnyW
JohnnyW

Reputation: 493

How to put a PHP array of strings into HTML input fields

I am trying to take elements from a php array and echo the results to a webpage in a list of text input fields. My problem is that the elements within $list are strings and for some reason the result of running the following code only puts the beginning of the string (up to the first space character) into the input field.

For instance, if one of my $list elements is "hello world", the corresponding input field only says "hello". How can I get the entire string to be inserted into the input field?

function parse_array($list)
        {
            $new_string = "";
            foreach($list as $element)
            {
                $new_string .= "<li><input type='text' size=60 value=" . $element . "></li>";
            }
            return $new_string;
        }

Upvotes: 0

Views: 1906

Answers (2)

Perry
Perry

Reputation: 11710

Instead of this:

$new_string .= "<li><input type='text' size=60 value=" . $element . "></li>";

Do this:

$new_string .= '<li><input type="text" size=60 value="' . $element . '"></li>';

When you need to use double qoutes in a string you should begin/end with single quotes that makes it much easier.

When you need to use single quote in a string you should begin/end with double quotes, that makes it easier for you.

When you need to use both you better use single quotes.

Also the thing with double quotes it will check if there is a variable inside the string. When you use single quote you must use ' . $variable . '

Also take a look at the manual for more information about strings in php

http://www.php.net/manual/en/language.types.string.php

Upvotes: 2

AbraCadaver
AbraCadaver

Reputation: 79024

You need to quote the value data just like you did type:

$new_string .= "<li><input type='text' size=60 value='" . $element . "'></li>";

Upvotes: 2

Related Questions