0

I am using a form in which people can order products. One of the variables of a product is this:

$product1 = 'Baguette Cheese';

I use this variable in the form like this:

<td><?php echo $product1 ?></td>
<td>&euro; <?php echo number_format($prijs[$product1], 2); ?></td>
<td><input type="text" name="<?php echo $product1 ?>" size="3" /></td>

Doing it this way, i can not read the name attrib. with $_POST because there is a space between Baguette and Cheese

This does work:

$product1 = 'Cheese-Burger';

But in the first td there is Cheese-Burger displayed and i do not want the hyphen between it. So i thougth: keep the hyphen away and strip the spaces in the name attrib like this:

<td><input type="text" name="<?php echo str_replace(' ', '', $product1) ?>" size="3" /></td>

Unfortunately, this is not working, he can not read the $_POST.

How can i make this work without using hyphen or underscore between Baguette and Cheese ?

When submitting:

if($_SERVER['REQUEST_METHOD'] == 'POST') 
{
echo $_POST[$product1]; // no result   
}
nuet maessen
  • 131
  • 10
  • POST data do support spaces and hyphens ? – adeneo Apr 18 '15 at 22:02
  • @adeneo, no, They need to be a single word, but can have numbers and underscores. – Pedro Lobito Apr 18 '15 at 22:12
  • You're only doing 1 `str_replace`. Meaning your are posting `$_POST['baguettechees'];` but you are reading `$_POST['baguette cheese']`! – DarkBee Apr 18 '15 at 22:17
  • @nuet Why are you giving the form `input name` the name of the product instead of simply `product1` ? then you can get it by using $_POST['product1']; . It doesn't make sense the form code. – Pedro Lobito Apr 18 '15 at 22:17
  • 1
    @PedroLobito - Nope, *HTML form names* can't have spaces etc. but POST data sent as x-www-form-urlencoded can have almost anything, and PHP also supports almost anything as an array key. – adeneo Apr 18 '15 at 22:21
  • It's to be able to post the form back in the database with the related products + amounts I guess. He should build up the forms as `';` though – DarkBee Apr 18 '15 at 22:21
  • 1
    `$_POST[$product1]` doesn't make any sense, when does the var `$product1` come from?! I guess you need to read more about forms, take a look here http://www.tutorialspoint.com/php/php_get_post.htm – Pedro Lobito Apr 18 '15 at 22:24
  • Well, i am not using a database. This form is only for mailing the choosen products. The main reason i want to do it this way, is that the user of the this orderlist **only** has to change the string and everything should be automatically changed also in the form – nuet maessen Apr 18 '15 at 22:26
  • You should avoid making it that way, because input name have to respect a [strict format](http://stackoverflow.com/a/3424964/2123530). – β.εηοιτ.βε Apr 18 '15 at 22:42

2 Answers2

1

Unfortunately, this is not working, he can not read the $_POST.

You aren't echoing the str_replace:

<td><input type="text" name="<?php str_replace(' ', '', $product1) ?>" size="3" /></td>

Change to:

<td><input type="text" name="<?php echo str_replace(' ', '', $product1) ?>" size="3" /></td>

Update after reading you comments:

Why are you giving the form input name the name of the product instead of simply product1 ? then you can get it by using $_POST['product1']; . It doesn't make sense the form code.

Try this instead:

<td><?php echo $product1 ?></td>
<td>&euro; <?php echo number_format($prijs[$product1], 2); ?></td>
<td><input type="text" name="product1" size="3" /></td>

then get the $_POST value by using:

$_POST['product1']
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • sorry, i forgot the echo befor it, but it is still not working. `echo $_POST[$product1];` displays nothing **Post above updated** – nuet maessen Apr 18 '15 at 22:09
-1

You could do:

$product1 = 'Baguette Cheese';

$test = explode(" ", $product1);

this will produce an array:

["Baguette","Cheese"]

then:

$nowhitespace = $test[0] . $test[1];

or

$nowhitespace = implode("", $test);
deowk
  • 4,280
  • 1
  • 25
  • 34