Jump to content

get _POST results when I don't know names?


turpentyne

Recommended Posts

I have a php loop that generates values from the database. I want to push their choice _POST into a string session variable, but how if I don't know the _POST name?

 

did that make any sense?

 

So I have this code (extracted from a loop):

$ExpandImageTemplate = <<<OutHTML

%4\$s <br>
<button type="submit" name="%3\$s" id="go" value="%2\$s">
<img src="%3\$s" width="147" height="34" alt="image to come" title="choice" />
</button>
OutHTML;

$Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars ($row['image_filepath']),htmlspecialchars ($row['component_name']));


 

I won't know what name each button will have when it submits. but I want to do something:

 

 session_start();

if(!isset($_SESSION['options_picked'])){
$_SESSION['options_picked'] = "";
} 

// here's where I'm stumped
if (isset($_POST["?????????"])) {

$_SESSION['options_picked'] .= $_POST["????????"];
}

 

 

Or maybe there's an easier way?

 

Link to comment
Share on other sites

POST data tends to come from a form... you would have created the form so you'll know the name of the field. Your question kind of doesn't make sense.

 

You firstly say your generating values from a database and then go on to say you want to set $_POST data to a $_SESSION variable. There is no link between the database and you wanting to set $_POST data to a $_SESSION variable.

Link to comment
Share on other sites

Forgive me for not knowing how to explain this. I'm new and I'm trying to do this with SprintF - something I've never worked with before. And maybe I'm going about this wrong..

 

The button name is written in PHP as: name="%3\$s"

 

The %3\$s value comes from the sprintf (in this case, $row['component_name']) :

$Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars ($row['component_name']),htmlspecialchars ($row['image_filepath']));

 

This value, in turn, comes from a query to the database. What they clicked on, could be one of a dozen possibilities, in this case.

 

I don't know what that key is, because I didn't set it. the database essentially determined it, via a loop.

 

When they make their selection, the page refreshes and I need to add that choice to my session variable.

 

 

 

Link to comment
Share on other sites

You can use the name of the input to create an array inside the $_POST array, so that you can easily loop through the content. All you have to do is to use [] as a part of the name, like this:

<input type="text" name="options[%3\$s]" value="%4$s">

Then you can just loop through all of the options like this:

foreach ($_POST['options'] as $key => $value) {

 

That said: Whenever you have variable names for input fields, chances are that you're doing things in an overly complex manner. Most of the time it's better to either use a static name, and act upon the content sent, or use another field to send the name you've tried to use as a name.

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.