Jump to content

Problem with $_POST


tmyonline

Recommended Posts

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

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

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.