0

I have a problem. I'm a newbie to PHP and am attempting to code an echo function for a product information form.

FORM USED FOR: This form is being used as a means to gather input text and checkbox data and echo it into another .php page so it can render/display that same collected form data into the form action="grid-maker.php" page with predefined html content, essentially creating the end result, which is a pinterest-type product info grid block that displays in it an img, the name of product, description, price, etc,...

PROBLEM

I am using the simple <?php echo $_GET[''] ?> to get things like the name, price, description... that was easy, the hard part for me is trying to use this echo function to deliver html content that varies in color and filled with input="" label="" div class=""'s using checkboxes that are checked, code example is shown below.

    <div class="checkbox-purple">
    <input type="checkbox" name="Color:_Purple"  id="sku0001-purple" value="Yes">
    <label for="sku0001-purple"></label>
    </div>

<div class="checkbox-orange">
    <input type="checkbox" name="Color:_Orange"  id="sku0001-orange" value="Yes">
    <label for="sku0001-orange"></label>
    </div>
</code>

This type of html is what I want to send over to the new .php page everytime a checkbox is checked but all my attempts have failed.

I tried this first:

<?php

    if(isset($GET['Color:_Purple']) && 
       $GET['Color:_Purple'] == 'Yes') 
    {
        echo "<div class='checkbox-purple'><input type='checkbox' name='Color:_Purple'  id='sku0001-purple' value='Yes' class='SKU0001_adjust'><label for='sku0001-purple'></label></div>";
    }

    ?>

this didn't work for me, I placed this code as is in between the div that I wanted it to be displayed in the product-maker.php page but it didn't show up.

After much newbie tinkering every which way I came to this

<?php echo "<div class='checkbox-purple'><input type='checkbox'   name='Color:_Purple'  id='<echo $_GET['sku']-purple' value='Yes' class='<echo $_GET['sku']_adjust'><label for='echo $_GET['sku']-purple'></label></div>"; ?>

this did work in injecting the code into the page, but it was not attached to the checkbox, nor am I confident about those single quotations inside the div's and stuff, not sure how this would affect the echo functions placed inside.

I tried many other methods that I have lost track of. Any and all recommendations would be helpful, and semi-detailed instructions would be appreciated since I'm new to PHP.

Thanks everyone.

WebEducate
  • 79
  • 2
  • 17
  • 1
    Make sure you sanitize anything you display, i.e. with `htmlspecialchars()`. Any user input that gets output without some sort of sanitizing/escaping is an XSS vulnerability. – Brilliand Jan 17 '14 at 00:43
  • You need javascript in order to do that. Something like [this](http://stackoverflow.com/questions/19734907/javascript-hide-show-div-on-checkbox-checked-unchecked) or [this](http://stackoverflow.com/questions/18421082/show-hide-div-if-checkbox-selected). – machineaddict Jan 17 '14 at 00:53
  • If you need the html to change when the client does something, you need a client-side code. i.e. javascript. php cannot do anything after the page is loaded. – jeff Jan 17 '14 at 00:57
  • could you elaborate for me, i'm unsure what to do here. – WebEducate Jan 17 '14 at 01:08
  • could anyone provide javascript that would do what I need? – WebEducate Jan 17 '14 at 01:57

1 Answers1

0

You can't access an array element in a string like that. PHP is good but won't understand your code.

Try it this way instead. Notice that the $_GET['sku'] is now wrapped in curly braces {$_GET['sku']}

echo "<div class=\"checkbox-purple\"><input type=\"checkbox\" name=\"Color:_Purple\" id=\"{$_GET['sku']}-purple\" value=\"Yes\" class=\"{$_GET['sku']}_adjust\"><label for=\"{$_GET['sku']}-purple\"></label></div>";
adamS
  • 592
  • 8
  • 14
  • thanks, that helped me with the single quotations, much appreciated. – WebEducate Jan 17 '14 at 01:02
  • no worries. backslash is a handy character for escaping characters e.g. `echo " \"quoted text will still print quotation marks\" ";` – adamS Jan 17 '14 at 01:20
  • what do i add to connect this echo to a checkbox after its been checked and submitted? – WebEducate Jan 17 '14 at 01:21
  • it's not possible to do this kind of thing with PHP because the script is executed on the server before it gets to the client. For that type of task you'll need to use JavaScript. It could easily be achieved with jQuery. – adamS Jan 17 '14 at 01:24
  • Ah i see, would you be so kind to provide an example or perhaps a link to another example using jquery? please and thank you. – WebEducate Jan 17 '14 at 01:29
  • Super thanks Adam, the link is what I am looking for for sure. Although I'm a newb it may take me a while to get it done. Thanks for your input everyone. Cheers! – WebEducate Jan 18 '14 at 00:35