Jump to content

Help with strings and arrays


mikejs

Recommended Posts

Hi I have a form with several input boxes I want to take the input into a string

 

$str = $_POST["box ++ box1 ++ box2 ++ box7 ++ box8"];

 

this is my table

 

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
  <p>
    <input type="text" name="box" value="" >    Box1<br />
  <input type="text" name="box1" value="" />    Box2<br />
  <input type="text" name="box2" value="" />    Box3<br />
    <label>
      <select name="box7" id="box7">
        <option value="1">Red</option>
        <option value="2">Blue</option>
        <option value="3">Green</option>
      </select>
    </label>
   Color<br />
<label>
  <select name="box8" id="box8">
    <option value="1">8px</option>
    <option value="2">10px</option>
    <option value="3">14px</option>
  </select>
</label>
Font size<br />
    <input type="submit" name="submit" value="Send">
  </p>
</form>

<?php

 

Any sugestions ? I would then like to take the $str and split it back into its individual ids using explode function maybe

 

thanks

 

M

 

Link to comment
https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/
Share on other sites

You can't do it like that. I think what you want to do is something like this: You're able to specify an array as the name for the elements in a form, then $_POST['fieldname'] would be an array containing the information from all the inputs. You can then do whatever you want with that.

 

ex

...
<input type="text" name="fieldname[]" />
<input type="text" name="fieldname[]" />
...
<?php
$array = $_POST['fieldname']; // Array('first input value', 'second input value');
?>

Thanks for the reply I have

 

    <input type="text" name="fname[]" value="" >    Box1<br />

  <input type="text" name="fname[]" value="" />    Box2<br />

  <input type="text" name="fname[]" value="" />    Box3<br />

  <input type="text" name="fname[]" value="" />    Box4<br />

  <input type="text" name="fname[]" value="" />    Box5<br />

  <input type="text" name="fname[]" value="" />    Box6<br />

 

with

$data = $_POST['fname'];

 

if I echo $data I should get (value,value,value, etc ?

Thanks for the reply I have

 

    <input type="text" name="fname[]" value="" >    Box1<br />

  <input type="text" name="fname[]" value="" />    Box2<br />

  <input type="text" name="fname[]" value="" />    Box3<br />

  <input type="text" name="fname[]" value="" />    Box4<br />

  <input type="text" name="fname[]" value="" />    Box5<br />

  <input type="text" name="fname[]" value="" />    Box6<br />

 

with

$data = $_POST['fname'];

 

if I echo $data I should get (value,value,value, etc ?

 

You can't echo it directly as $_POST['fname'] is an array.  So, you'll need to loop through it:

 

foreach ($_POST['fname'] as $value)
{
   echo "$value ";
}

To see the contents of the array (for debugging or whatever) use print_r. If you want to just display the contents of the array as a string with a comma as the delimiter you can use implode()

ex

...
echo implode(', ', $data);

 

Hi I tried that I get Warning: implode() [function.implode]: Invalid arguments passed ??

Are you sure $data is an array?

 

I have the following

 

if(isset($_POST['submit'])) {
$str = $_POST['fname'].' ++ '.$_POST['fname2'].' ++ '.$_POST['fname3'].' ++ '.$_POST['fname4'].' ++ '.$_POST['fname5'].' ++ '.$_POST['fname6'].' ++ '.$_POST['fname7'].' ++ '.$_POST['fname8'];
do_it($str);
}

?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

 

So in my case I have $str as my array

 

 

Can you post the code in context please? In that example implode() isn't being used. And $str isn't even an array unless do_it() takes $str be reference and changes it into an array? I can't really be much of any help unless you post the actual code in question.

Hi sorry its a string not an array

 

I kind of have it working now all the inputs are stored in $str as a string how can I add a / charecter in between each group

 

box 1 www box 2 eee current output wwweee I want the output to be www/eee/ etc

 

at the reciving end I want the $str to be put back into varibles that i can use to populate a table

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.