Solved

PHP : Returning a Variable Inside a $_POST variable

Posted on 2010-09-21
8
595 Views
Last Modified: 2012-05-10
Hi Experts:

A logical problem here - I need to place a string returned from a database in to a POST var to match up with POST variables from a form in order to access the data therein.

Can you please explain how this can be done?

Example :

      $_POST[''.$my_value["name"].'']

Will this work?
0
Comment
Question by:wilson1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 33724352
Yes will work for the time of the execution of the php -script. It will not modify the form/HTML form where data was submitted.

$_POST is no read only superglobal. It's just like any other array.
0
 
LVL 3

Expert Comment

by:Progressed
ID: 33724353
why do you want to do this? post more of the script because i dont see WHY you want to use it as a POST... you already have the variable!
0
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 33724370
Same thing but prevents error

$var=$my_value["name"];

if(isset($_POST[$var]) && $_POST[$var]!='')
  $postedVar=$_POST[$var];


Hope this helps
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:vpx
ID: 33724385
Example:
Your form:
 
<form action="valider.php" method="post">
<select name="object">
...
<input name="gender" type="text" />

The valider.php file:
<?php
$object = $_POST['object '];
$gender = $_POST['gender'];
?>

In this example, the php file is receiving two variables from a list and a text box but you can change those inputs.

Hope it helped.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33724403
Make sure you read the variable value from the form. otherwise, you cannot get the value straight from the variable. Just check the form name variable with array variable. It should work.

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 33724583
Hi,

you need this??:

 print_r($_POST);
 echo " <br>";
 $_POST['var_name']=$my_value["name"];
 print_r($_POST);
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33725156
Install this and run it to see the moving parts.  HTH, ~Ray
<?php // RAY_post_example.php
error_reporting(E_ALL);
echo "<pre>\n"; // MAKE IT EASY TO READ



// THIS ILLUSTRATES THE RELATIONSHIP BETWEEN THE 'ACTION' AND THE 'FORM' WHEN COMBINED INTO A SINGLE PHP SCRIPT FILE



// SET A DEFAULT VALUES FOR A VARIABLE TO PREPOPULATE THE FORM - THIS COULD COME FROM A DATA BASE, OR MIGHT BE EMPTY
$my_INPUT_Field = 'ORIGINAL DATA';

// IF THE FORM WAS FILLED IN, COPY THE INPUT INTO OUR VARIABLE
if (!empty($_POST["my_INPUT_Field"]))
{
    $my_INPUT_Field = $_POST["my_INPUT_Field"];
}


// HAS ANYTHING BEEN POSTED - IF SO, $_POST IS SET AND CONTAINS THE DATA
if (!empty($_POST))
{
    // SHOW THE POST ARRAY
    var_dump($_POST);
}
// THIS IS THE END OF THE ACTION SCRIPT




// THIS IS THE FORM SCRIPT - FOR CONVENIENCE WE DROP OUT OF PHP INTO HTML
?>
<form method="post">
TYPE SOMETHING HERE:
<input type="text"   name="my_INPUT_Field"   value="<?php echo $my_INPUT_Field; ?>" />
<input type="submit" name="my_SUBMIT_Button" value="go" />
</form>

Open in new window

0
 
LVL 4

Author Closing Comment

by:wilson1000
ID: 33726103
Thank you! Just what I needed
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question