0

I have a php variable "echo $id". Now I want to use the $_POST method to post the variable. I just want to know how to do this for a variable because $_POST[$id] does not work?

3
  • 2
    I'm afraid you'll have to get acquainted with some concepts first. Commented Dec 17, 2011 at 13:34
  • say what?? Post where ? variable echo $id ?? Commented Dec 17, 2011 at 13:35
  • What do you mean by posting the variable? $_POST gets data out of a form into a variable. And it's not a method, $_POST is a variable itself. Commented Dec 17, 2011 at 13:35

6 Answers 6

2

I think you are misunderstanding a basic concept here.

The $_POST super global is used to receive input (in the form of a POST request) from the user. While it is possible to set variables in it, you shouldn't.

Sign up to request clarification or add additional context in comments.

Comments

1

Your question does not make sense. If you have an HTML form:

<form action="" method="post">
    <input type="text" name="something" />
    <input type="submit" value="Submit" />
</form>

Then you get the variable $_POST['something'] with whatever the user typed in the text box.

On its own, $_POST is just a variable like any other. You can assign to it $_POST['test'] = 123;, you can delete from it unset($_POST['test']);, you can even make it something other than an array $_POST = "Hello, world";, it just happens to be pre-populated with form data, if any.

Comments

0

With the method $_POST you must be posting to something.

My suggestion to you is to create a form, then have the form going to the file you wish to post to:

So something like this:

echo '<form action  = "fileToPostTo.php" method = "post">
<input type = "text" hidden value = "'.$id.'" />
</form>';

And then submit the form when the document loads through jquery or javascript.

Comments

0

You can do it by $_POST['id'] = $id (then You will have it in $_POST['id'] variable (but You shouldn't do it :P).

Or You can send $id by form. Like example:

<form action="/pageToPOST.php" method="post">
  <input type="text" value="<?=$id ?>" name="id" />
  <input type="submit" name="" value="submit it!" />
</form>

And You'll have $_POST['id'] on http://yourdomainname.com/pageToPOST.php page

Comments

0

you can get and pass the value without page load and form.

 <input type="text" name="something" id="something" />
 <input type="button" value="ok" onclick="value();"/>
 function value()
 {
 var something=$("#something").val();
 var dataparam="oper=show&something="+something;
  $.ajax({
      type:"post",
      url:"yourphpname.pnp",//this is very important.
      data:dataparam,
      success:function(data)
   {
   alert(data);
   }
   });
    }
     $oper =(isset( $_REQUEST['oper'])) ?  $_REQUEST['oper'] : '';
          if($oper == "show" and $oper != '')
          {
         $something=$_REQUEST['something']
         echo $something;
         }

Comments

0

what you want to do is assign a value submitted to your script using the POST method, to your $id variable. Something like:

$id = $_POST['id'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.