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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.