tmyonline Posted February 27, 2008 Share Posted February 27, 2008 Hi guys: Suppose I have a form like this: <?php if (isset($_POST[name])) { echo "Hello " . $_POST[name]; } ?> <html <head></head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <input type="text" name="name" /> <input type="submit" name="submit" value="submit"> </form> </body> </html> This form works fine. Now, the problem is that the name attribute is not as simple as "name", a lot of times, it can be a string with spaces in between. As such, when I try, say: if (isset($_POST['diet coke'])) { ... } This code does not work because the name attribute "diet coke" contains a space between "diet" and "coke". If in the form, I write: name="diet_coke", then $_POST['diet_coke'] should store the name value once the submit button is clicked. Because of the fact that the name "diet coke" will appear on the menu, I don't want it appear as "diet_coke" with the underscore. So, in the case that the name attribute is a space separated string, like "diet coke", how should I handle it so that $_POST['diet coke'] will contain the name value (once the submit button is clicked) ? I have a long list of items and I cannot tell users to put a hyphen or an underscore in every space separated item. I did try using a variable, say, $item = "diet coke" with the hope that $_POST[$item] will work but it didn't help. Thanks guys! Link to comment https://forums.phpfreaks.com/topic/93379-problem-with-_post/ Share on other sites More sharing options...
sKunKbad Posted February 27, 2008 Share Posted February 27, 2008 $_POST['diet_coke']='diet coke'; Here, the value of $_POST['diet_coke'] will be 'diet coke', so if you want to echo 'diet coke' you could just echo $_POST['diet_coke']; Make sense? Link to comment https://forums.phpfreaks.com/topic/93379-problem-with-_post/#findComment-478317 Share on other sites More sharing options...
sKunKbad Posted February 27, 2008 Share Posted February 27, 2008 I guess you would need to know that whatever is posting to this page is responsible for setting the value of $_POST['diet_coke']. Whether it is a form or a cURL script or an AJAX script, the code that posts to this page decides the value of $_POST['diet_coke]. <input type="text" name="diet_coke" value='diet coke' /> Link to comment https://forums.phpfreaks.com/topic/93379-problem-with-_post/#findComment-478320 Share on other sites More sharing options...
wildteen88 Posted February 27, 2008 Share Posted February 27, 2008 Your browser should automatically convert spaces to an underscore if a field name contains spaces. So if your field is named diet coke then you should be able to access it via PHP using $_POST['diet_coke']; Link to comment https://forums.phpfreaks.com/topic/93379-problem-with-_post/#findComment-478328 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.