1
<form action="actions/add_cat.php" method="post" id="rtf" name="">
    <input type="text" name="cat_title" id="cat_title" required="required" placeholder="Category Title"/>

    <br /><br />


    <button type="button" onclick="button_click('#d31b26');" value="d31b26" name="cat_color"><div class="redSelect"></div></button>
    <button type="button" onclick="button_click('#f9c04c');" name="cat_color" value="#f9c04c"><div class="yellowSelect"></div></button>
    <button type="button" onclick="button_click('#72bce9');" name="cat_color" value="#72bce9"><div class="blueSelect"></div></button>
    <button type="button" onclick="button_click('#ec9292');" name="cat_color" value="#ec9292"><div class="pinkSelect"></div></button>
    <button type="button" onclick="button_click('#b7d04e');" name="cat_color" value="#b7d04e"><div class="greenSelect"></div></button>    


    <div class="indexBox">
        <div class="indexBoxHeader" id="box">
            <i class="fa fa-question-circle" style="font-size: 2em;"></i></div>
        <div class="indexBoxFooter">
            <div class='printchatbox' id='printchatbox'></div>
        </div>
    </div>



    <br><br> 
    <input onclick="formsubmit()" type="submit" value="Create Category" name="submit"/>


</form>

If i test using a text input and using the cat_color name it will post that entry but when using the above it does not seem to to take the item I am selecting?

Here is the post query incase you need to see it:

$sql = "INSERT INTO cat_list (cat_title, cat_color) VALUES ('".$_POST["cat_title"]."', '".$_POST["cat_color"]."')";
2
  • You shouldn't use a plain concatenation of $_POST["cat_title"] as one can easily craft a malicious request. See about en.wikipedia.org/wiki/SQL_injection. You should look at mysql_real_escape_string() or/and mysql_prepare (I assume you use mysql but you find equivalents for other languages) Commented May 22, 2015 at 20:31
  • Ignore that for now this is for testing purposes Commented May 22, 2015 at 20:33

3 Answers 3

2

As I can't comment.

Try to have <radio> buttons instead of normal buttons. So the php/form would understand it is and option.

Example:

<form .. >
   ..
   <input type="radio" name="cat_color" value="#d31b26"><div class="redSelect"></div><br>
   <input type="radio" name="cat_color" value="#f9c04c"><div class="yellowSelect"></div><br>
   ..
</form>

EDIT

<form action="actions/add_cat.php" method="post" id="rtf" name="">
    <input type="text" name="cat_title" id="cat_title" required="required" placeholder="Category Title"/>

    <br /><br />


    <!-- Radio Buttons With customized color class -->
   <input type="radio" name="cat_color" value="#d31b26" class="redSelect"><br>
   <input type="radio" name="cat_color" value="#f9c04c" class="yellowSelect"><br>
   <input type="radio" name="cat_color" value="#72bce9" class="blueSelect"><br>
   <input type="radio" name="cat_color" value="#ec9292" class="yellowSelect"><br>
   <input type="radio" name="cat_color" value="#b7d04e" class="greenSelect"><br>


    <div class="indexBox">
        <div class="indexBoxHeader" id="box">
            <i class="fa fa-question-circle" style="font-size: 2em;"></i></div>
        <div class="indexBoxFooter">
            <div class='printchatbox' id='printchatbox'></div>
        </div>
    </div>



    <br><br> 
    <input onclick="formsubmit()" type="submit" value="Create Category" name="submit"/>


</form>
Sign up to request clarification or add additional context in comments.

13 Comments

May I ask what you mean by 'normal' buttons?
<button type="button" .. /> or <input type="button" .. />
If I change it to an input type the value shows itself on the button though?
Ah, I see, try using the radio buttons, and then put the color by the side of it.
And can you post how you get the values from the post please.
|
0

So in the in the end I just wrapped a hidden field inside the div for my colour picker like so:

<div class="redSelect" onclick="button_click('#d31b26');"><input  type="hidden" name="cat_color" value="#f9c04c" ></div>
<div class="yellowSelect" onclick="button_click('#f9c04c');" ><input type="hidden" name="cat_color" value="#f9c04c" ></div>
<div class="blueSelect" onclick="button_click('#72bce9');"><input type="hidden" name="cat_color" value="#72bce9" ></div>
<div class="pinkSelect" onclick="button_click('#ec9292');"><input type="hidden" name="cat_color" value="#ec9292"></div>
<div class="greenSelect" onclick="button_click('#b7d04e');"><input type="hidden" name="cat_color" value="#b7d04e" ></div>

works a treat and posts out the value of the hidden field

Comments

-1

With me, happened that my form had so so many inputs that it simply didn't send the last ones (the button and some others). In some way, there is a "limit" on how much data you may send by POST.

Then, I put a hidden input in the upper part of the form with the same name of button and it worked 👍🏻.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.