0

Hi friends am trying to pass the input type as array.

Here is my code..

<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo sizeof($videoid);
?>
}
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>

But am unable to read them for example I want to pass hello,how,are,you,why but am unable to read them after when I pass them

6 Answers 6

0

Here's how I would do it. You only need one texbox. No need for multiple textbox. Try this:

<?php
    if(isset($_POST['submit_tags'])){
    $tagsList=$_POST['tags_list'];
    $videoids = explode(",", $tagsList);
    echo sizeof($videoids);
    }
    ?>

Also you can remove the [] from your textbox name name="tags_list[]" to name="tags_list"

0

1) To send a input value as array to PHP, you need to set one value per input

<input type="text" name="tags_list[]" value="hello">
<input type="text" name="tags_list[]" value="world">

Now the PHP can understand $_POST['tag_list'] as array.

2) You can change your approach and split the string.

<input type="text" name="tags_list" value="hello, world">

And transform the $_POST['tag_list'] string into an array. Example:

$tag_list = explode(', ', $_POST['tag_list']);

Or you can use preg_split function to add more intelligence to your string transformation.

Regards,

0

To read(convert string) from array you can use implode

<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo implode(",",$videoid);
}
?>

<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
1
  • Yeah but the user in input enters like hello,how,are,why so how can I use ot to generate multiple inputs Mar 8, 2017 at 5:40
0

Sravya try to understand the concept first, name as an array in a html element is used when you want to make more than one html element as same type so that you can get its value by iterating the array like:

<input type="text" name="tags_list[]"> -> its value is one
<input type="text" name="tags_list[]"> -> its value is two
<input type="text" name="tags_list[]"> -> its value is three

You can print its values like:

print_r($tags_list);

Otherwise use single html element.

1
  • Break it by space or something else Mar 8, 2017 at 5:40
0

try this.

$variableAry=explode(",",$variable); //you have array now
            foreach($variableAry as $var)
            {
                echo $var. "<br/>";
            }
0
<?php
if(isset($_POST['submit_tags'])){

    $videoid=$_POST['tags_list'];
    $tag_list = explode(', ', $videoid);
    print_r($tag_list);

}
?>

    <form action="" method="post" enctype="multipart/form-data">
    <input type="text" name="tags_list[]">
    <input type="text" name="tags_list[]">
    <input type="text" name="tags_list[]">
    <input type="submit" name="submit_tags">
    </form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.