Jump to content

Single Input Name and Multiple Values


Go to solution Solved by maxxd,

Recommended Posts

I have the form:

 

<form>

What name people call you: <input type="text" name="firstname" value="xxx"><br>

Your favorite cars are: <input type="text" name="brand" value"yyy">

</form>

 

Send to a php engine for processing.

 

 

 

Insted of ($_GET["brand"]) to have only "yyy" value I want to have three values for the same input

 

brand[0] = "yyy"

brand[1] = "mini"

brand[2] = "pontiac"

 

 

 

TO work in three different links:

 

http://($_GET["brand[0]"]).com                  html result:  http//yyy.com

 

http://($_GET["brand[1]"]).com                  Html result   Http://mini.com

 

http://($_GET["brand[2]"]).com                  Html resut    http://pontiac.com

 

 

 

 

I want to use this kind of system to implement multiple website filters in only one form and make available the link to each one aleardy withh the fillters settings, insted of going to each website and type the same filters again.

 

This is the basic that I need make. How can I do this small script?

Link to comment
https://forums.phpfreaks.com/topic/289363-single-input-name-and-multiple-values/
Share on other sites

You could add 3 input fields and name them using array syntax. Here is a quick example:

<?php
if(isset($_GET['brand'])) {
    print '<pre>' . print_r($_GET, true) . '</pre>';
}
?>
<form method="get">
    <p>Your favorite cars are:</p>
    <div><label>Brand 1: <input type="text" name="brand[]"></label></div>
    <div><label>Brand 2: <input type="text" name="brand[]"></label></div>
    <div><label>Brand 3: <input type="text" name="brand[]"></label></div>
    <input type="submit">
</form>

A couple options in addition to cyberRobot's suggestion come to mind. You could use a checkbox group and a per-determined list of options, which is probably the safest and most controllable method. Or you can explode the $_POST['brand'] value on a comma (or other character). This, however, leaves you at the mercy of your users and whether or not they're going to read and follow the instructions.

Edited by maxxd

The whole problem still doesn't make a lot of sense.

 

You have a text field, which means the user can enter anything they want. How do you know they will enter exactly three brands? How do you know you can just append “.com” and get a valid URL? And why do you predefine the favorite brands? 

Is not the answer what I am looking for.

 

Jacques1, the text field is only for example, I will use radio buttons.

 

 

To be more explicit, when you buy a car you have more filters:

 

NEW    &  USED   (for this we create radio buttons for the user to chose)

 

 

There are many website (3 for example) that have a primary link:

 

http//ebay.com

http//cars.com

http//auto.com

 

 

For New cars the value information needed to each website it s different, for example

 

http//ebay.com/newcars/

http//cars.com/new/

http//auto.com/newmake/

 

The user Select NEW (radio Buttons) and a php engine will add the above information for each link, having a predetermined information:

 

NEW = new[0] = "newcars/"

NEW = new[1] = "new/"

NEW = new[2] = "newmake/"

 

The result script for  will be:

 

href="http://ebay.com/$_GET(new[0])        ==== >  http//ebay.com/newcars/

 

 

This I will want to do for every link, if is possible.

 

I hope now everything is more clear.

There absolutely no reason for putting this data into the form. The purpose of the form is to let the user choose a filter. So in your case, the radio button values would be “new” and “used”.

 

What you do with the user choice is a completely different story. In the processing script of the form, you can create as many links as you want. But you don't do this in the form.

There is nothing to split. As I've already tried to explain, you only need a single value for the radio buttons. In your example, the value is either “new” for new cars or “used” for used cars.

 

After you've received the submitted data, you're free to create your links. But it's absolutely pointless to have the links as form values.

Where the data comes from is irrelevant for this problem. The point is that you do all your link logic after the user has made their choice.

 

The user only tells you “I want used cars” or “I want new cars”. Based on that decision, you then fetch the links from an array, a database or whatever.

No database, because is not so much data, but yes is also a good option what you say. I try to make this simple as possible.

 

This is a very crude example, but you could hard-code the values in an array. Then use a GET variable from a form, or wherever, to pull the corresponding links.

<?php
$linkArray = array(
    'new' => array(
        'http//ebay.com/newcars/',
        'http//cars.com/new/',
        'http//auto.com/newmake/'
    ),
    'old' => array(
        'http//ebay.com/oldcars/',
        'http//cars.com/old/',
        'http//auto.com/oldmake/'
    )
);
 
foreach($linkArray[$_GET['type']] as $currLink) {
    print "<div>$currLink</div>";
}
?>

If the GET variable (type) is set to "new", the example code will display the new links. Otherwise, it will display the old links when set to "old".

  • Solution

You need to reconsider your approach. By trying to 'make things as simple as possible', you're twisting things way out of control. Consider using comboboxes for the your filters.

<select name='type'>
    <option value='newcars'>New Cars</option>
    <option value='new'>New Whatever</option>
    <option value='newmake'>New Make</option>
</select>
<select name='year'>
    <option>2014</option>
    <option>2013</option>
    <option>2012</option>
</select>
<select name='color'>
    <option value='blue'>Blue</option>
    <option value='red'>Red</option>
    <option value='rusted'>Rusted Out</option>
</select>

Then you build it in php

$link = "http://ebay.com/{$_GET['type']}/{$_GET['year']}/{$_GET['color']}/";

I've honestly never used ebay so I don't know if that link makes any sense at all to it, but I think it's close to what you're trying to do?

Yes maxxd the last part is what I want:

 

$link = "http://ebay.com/{$_GET['type']}/{$_GET['year']}/{$_GET['color']}/";

 

But the first part is the problem. Because in the form should be only 2 option, that will have more values in the php script.

 

The solution will be this:

 

<?php
$linkArray
= array(
    'new' => array(
        newcars/',
        new/',
        newmake/'
    ),
    'old' => array(
        'oldcars/',
        /old/',
        'oldmake/'
    )
);
 
?>

 

But how can I take each array and put it in the link, because in "new" they are 3 array, how can I take a specific one and add to the link

 

$link = "http://ebay.com/{$_GET['new']}/{$_GET['year']}/{$_GET['color']}/"

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.