SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing multiple values as single value

    It's been awhile since I've done any php, so this might be a very obvious fix, but I've searched all over and can't seem to find a solution.

    What I'm trying to do is a little precise which is part of what's giving me trouble.

    I'm working within zencart, so I'm trying to add as little code as possible while still achieving my goal.

    I've built a custom form within the checkout page that asks the customer to chose how they were referred to the site. There are six options, the first two are radio button only, and the last four are radiobuttons that open up to textboxes when selected. I'd like to be able to pass both the radio button and the text value through to the next page as a single value, 'refer'. I can't figure out if there's actually a way to do this. I know that I could create variables and produce a string, but this 'refer' value is passed along through the several other pages that I've had to bring it to. I basically copied zencart's treatment of a 'comments' value in passing this. Here is my code so far, I'm not including the javascript portion as I don't think you really need it:

    PHP Code:
    <table style="padding-left: 15px;">
    <tr><td>Magazine</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer" value="Magazine" onclick="noshow()" /></td>
        <td></td>
    </tr>
    <tr><td>Web Search</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer" value="Web Search" onclick="noshow()" /></td>
        <td></td>
    </tr>
    <tr><td>Friend</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer" value="A Friend" onclick="rad1()" /></td>
        <td><input type="textfield" id="check1"  name="refer_box"  value-"<?php echo  $_POST['refer_box'];?>"/></td>
    </tr>
    <tr><td>Blog</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer" value="Blog" onclick="rad2()" /></td>
        <td><input type="textfield" id="check2"  name="refer_box" value-"<?php $_POST['refer_box'];?>"/></td>
    </tr>
    <tr><td>Website</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer" value="Website" onclick="rad3()" /></td>
        <td><input type="textfield" id="check3"  name="refer_box"  value-"<?php $_POST['refer_box'];?>"/></td>
    </tr>
    <tr><td>Other</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer" value="Other" onclick="rad4()" /></td>
        <td><input type="textfield" id="check4"  name="refer_box"  value-"Other :<?php $_POST['refer_box'];?>"/></td></tr>
    </table>
    Is there a way to add the $_POST['refer_box'] value into the radiobutton value? Or if there's a way to place text into the textfield values and create a string that combines it and the entered text.
    If I can rework it with variables but still have it leave the page as 'refer', that would work as well.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    name="refer_box[]"

    A radio button however, can only have 1 selected between the same group.

    For a checkbox/select this is applicable

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi wonshikee,

    Thank you for the response. I guess I'm not really following you though?

    I think perhaps I explained what I want wrong?

    You're saying that I should make the name for the textboxes "refer_box[]" so they'll capture an array?

    I only want to capture one radio button along with it's accompanying textbox. The trick is to somehow change the value of the input field to represent the value of both in a string.

    For example, if a user chooses "Website" for it's radiobutton, then the it's textbox with come up prompting for a specific website such and "sitepoint.com".
    Really what I want in this case is for the "refer" value to be "Website : sitepoint.com".

    Now, I know that the ideal way to do this would be to create a variable outside of the form that puts these together, but there are several files that need to use "refer" since this is zencart. The only other option I can think of is to send "refer_box" all the way to the database, and then pull both values when displaying, but I'd like to keep from having to do that.

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I think I understand now... sort of

    The way I would do that is by mapping it with the radio button's value.

    <input type='radio' name='refer' value='A Friend' />
    <input type='text' name='refer_text[A Friend]' />

    Now on the PHP side,

    You would retrieve the "refer" with $_POST['refer'], and the linked text box would be $_POST['refer_text'][$_POST['refer']], does this make sense?

    A better way to build this, is simply with a php array ('A Friend','Blog','Website',....), then using foreach loop to output all the HTML. This makes maintence much much much easier, but this is simply an advice.

    Also just letting u know you have a typo in your code, it says value-, instead of value=

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha! Wow! Completely missed that.

    I decided to go ahead and enter both as separate values to be entered into the database.

    I'm still having a problem passing the textbox for some reason, when I saw the typo I thought it had all been solved. But no such luck.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gah!

    I've got them passing now as separate values to the database. The problem I'm getting now is that since the $_POST['refer_box'] can only pull from one textbox as it is, it's only pulling from the last one, thereby cancelling out any information, should the 1st, 2nd, or 3rd textbox be chosen instead.

    So now this is what I have my form looking like:
    PHP Code:
    <table style="padding-left: 15px;">
    <tr><td>The Next Big Zing</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer_btn" value="The Next Big Zing" onclick="noshow()" /></td>
        <td></td>
    </tr>
    <tr><td>Web Search</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer_btn" value="Web Search" onclick="noshow()" /></td>
        <td></td>
    </tr>
    <tr><td>Friend</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer_btn" value="A Friend" onclick="rad1()" /></td>
        <td><span id="check1">You're friend's name?:<input type="textfield" name="refer_box"  id="text1" value="<?php $_POST['refer_box'];?>"/></span></td>
    </tr>
    <tr><td>Blog</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer_btn" value="Blog" onclick="rad2()" /></td>
        <td><span id="check2">Which Blog?:<input type="textfield" name="refer_box"  id="text2" value="<?php $_POST['refer_box'];?>"/></span></td></td>
    </tr>
    <tr><td>Website</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer_btn" value="Website" onclick="rad3()" /></td>
        <td><span id="check3">Which Website?:<input type="textfield" name="refer_box" id="text3"  value="<?php $_POST['refer_box'];?>"/></span></td>
    </tr>
    <tr><td>Other</td>
        <td style="padding-left: 15px;"><input id="radio" type="radio" name="refer_btn" value="Other" onclick="rad4()" /></td>
        <td><span id="check4">Where?:<input type="textfield" name="refer_box"  id="text4" value="<?php $_POST['refer_box'];?>"/></span></td></tr>
    </table>
    I've tried naming the refer_box as an array; refer_box[friend], refer_box[blog] and so on, but in that case the value returns as just "Array". Furthermore, I would like to have one single value for 'refer_box' chosen when it gets passed on to the next page. Is there a way to do this?

  7. #7
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Re-read what I wrote instead of just skimming it.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did work a little with a foreach loop, but couldn't quite get it, my php skills are not there yet. lol

    I finally decided to use javascript to just change the name of my textboxes according to which radiobutton was selected(which I already had in place anyway).

    Thank you for all of your help though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •