0

I'm having trouble adding the value of type="button" form elements to a mySql database, and I'm wondering if I'm missing something.

Edit - It doesn't look like the information for that element is being passed from the html to the php because it's not echoing a value. My only problem is with this one element and the rest of the form is being submitted properly.

I'm using this for an online quiz which builds a user profile based upon images they've selected, and am setting the images as background images for the button elements, and I'm trying to do this in straight html (as opposed to using javascript together with radio buttons or check boxes).

<input type="button" name="quiz_start" value="jeans" style="background: url(files/start1.jpg) no-repeat; width:54px;height:140px; cursor:pointer; border:none; color: transparent; font-size : 0">       

I've simplified the php code for purposes of asking the question (including specifying the user id and limiting it to only one field). I've also included the full code below.

<?php
//Start session & connect to database 
$user_id = 3;   

$qry = "INSERT INTO style(user_id, quiz_start) VALUES('$user_id','$_POST[quiz_start]')";
$result = @mysql_query($qry);      
header("location: page2.html");
exit(); 
?>

The full query is:

$fieldlist=$vallist='';
foreach ($_POST as $key => $value) { 
 $fieldlist.=$key.',';
 $vallist.='\''.($value).'\',';
}
$fieldlist=substr($fieldlist, 0, -1);
$vallist=substr($vallist, 0, -1);
$fieldlist.=', user_id';
$vallist.=','.$user_id;
$setlist='';
foreach ($_POST as $key=>$value){
$setlist.=$key .'=\''.$value.'\',';
}
$setlist=substr($setlist, 0, -1);  
$result = mysql_query('UPDATE style SET '.$setlist.' WHERE user_id='.$user_id);
if (mysql_affected_rows()==0) {
$result = mysql_query('INSERT INTO style ('.$fieldlist.') VALUES ('.$vallist.')');
}  
header("location: page2.html");
exit();
?>
Chaya Cooper
  • 2,566
  • 2
  • 38
  • 67
  • 2
    We cannot tell what the problem is unless you actually post your PHP code where the queries to your database are. Please include all your relevant code to get a specific answer. – Devon Bernard Jan 14 '13 at 02:51
  • It's just a simple insert query so I didn't think that it was important to include, but I'll add that now – Chaya Cooper Jan 14 '13 at 03:07
  • Are you closing this in with `
    ` tags?
    – ROY Finley Jan 14 '13 at 03:10
  • @ChayaCooper: That may be true; but if your database is not being updated their is either an error with your form post, data handling, or your query. And we need to see all that relevant code in order to figure out where the problem is. – Devon Bernard Jan 14 '13 at 03:10
  • yes - and the rest of the form is submitting properly :-) – Chaya Cooper Jan 14 '13 at 03:10
  • 1
    Are you getting a value for `$_POST['quiz_start']` if you echo it out? – ROY Finley Jan 14 '13 at 03:13
  • @DevonBernard: I just added a simplified version of my INSERT query. Please let me know if there's any additional code which would be helpful – Chaya Cooper Jan 14 '13 at 03:17
  • 1
    Aren't you missing the quotes on the post vector? Shouldn't it be $_POST['quiz_start'] ? – Afonso Tsukamoto Jan 14 '13 at 03:19
  • @ROYFinley - No, the php isn't echo'ing any value for it :-( Any thoughts? – Chaya Cooper Jan 14 '13 at 03:26
  • @ChayaCooper: If $_POST['quiz_start'] is not echoing in the opening I feel like when you click your button your form is not actually submitting. – Devon Bernard Jan 14 '13 at 03:29
  • @DevonBernard That's why I'm confused, because the rest of the form is being submitted properly – Chaya Cooper Jan 14 '13 at 03:34
  • @ChayaCooper: Wait so you are saying the rest of the values in your form are submitted correctly but the button value is not? That is interesting, in that case I would create a hidden value then just have the button for the visual UI appearance. – Devon Bernard Jan 14 '13 at 03:36

3 Answers3

1

Try this:

    <?php
//Start session & connect to database 
$user_id = 3;   

$qry = "INSERT INTO style(user_id, quiz_start) VALUES('".$user_id."','".$_POST['quiz_start']."')";
$result = @mysql_query($qry);      
header("location: page2.html");
exit(); 
?>
ROY Finley
  • 1,406
  • 1
  • 9
  • 18
  • Since it's not echoing any value for quiz_start in the php, that didn't do the trick :-( @DevonBernard mentioned that buttons can't seem to be used to capture the user's selection. Do you know a way that they can? Otherwise, I'll go the javascript route with radio buttons/check boxes. – Chaya Cooper Jan 14 '13 at 04:28
1

Seeing that you are unable to echo $_POST['quiz_start'] that means your value is not actually set. This is because when you use a class button as in <input type='button'> your form is not actually submitted like <input type='submit'>

One solution would be to change your button to an actual submit and format that... or you need to call a javascript function with an onClick from your button as in:

<input type="button" onClick="myfunction()">

For reference to what I am talking about look at this post.

If as you say the rest of the form values are submitting fine but just the button value is not working you have a few different possible solutions depending on your preference.

  1. Use a select field or checkbox for people to select a type in which you can pass your data.
  2. Submit your form in javascript with <input type="button" onClick="myfunction()"> then running your update query in javascript.
  3. Finally if you still want to run your query in PHP you can run a javascript function to make an AJAX call to return JSON information in which you can define a php variable after the page has loaded in which you can then plug into your update query.
Community
  • 1
  • 1
Devon Bernard
  • 2,250
  • 5
  • 19
  • 32
  • I assumed it was inside a
    and that he did submit the form after the button. That way it would work, right?
    – Afonso Tsukamoto Jan 14 '13 at 03:41
  • @AfonsoTsukamoto - That's exactly what I'm doing :-) This is in the middle of the form, and I have a separate submit button (which is why the rest of the form is submitting properly) :-) – Chaya Cooper Jan 14 '13 at 03:42
  • Alright that makes sense now, and yes if she includes this hidden value in the form it should carry the data just fine. Unless she is trying to only have this value set when the button is clicked and then the form is submitted; and wants no value when the form is submitted and the button was not clicked. – Devon Bernard Jan 14 '13 at 03:45
  • @DevonBernard: Is there a way to just capture the value of each button element that's been selected directly instead of creating separate hidden elements for each one? – Chaya Cooper Jan 14 '13 at 03:47
  • Sorry @ChayaCooper, assumed you were a 'he'. My bad :P Good luck! – Afonso Tsukamoto Jan 14 '13 at 03:49
  • 1
    I think in a coding perspective the purpose of a button (not submit button) is to call something, not necessarily pass data through a submission process. But if you want the data to only be passed when selected a select input or checkbox would be a optimal solution assuming you are ok with that look. – Devon Bernard Jan 14 '13 at 03:49
  • @DevonBernard, in that case, javascript on the submit with a hardcoded false to run a the javascript before submit and document object manipulation would do it, right? (I've been doing some research on javascript/html/css for my next semester, why I'm so curious:P) – Afonso Tsukamoto Jan 14 '13 at 03:49
  • @AfonsoTsukamoto: No need to apologize ;-) – Chaya Cooper Jan 14 '13 at 03:51
  • @AfonsoTsukamoto: Well yes you could call a javascript function to run from that button but that giver chaya two options; either make her update query in javascript; or within that function to make an AJAX call for a JSON return to set a PHP variable in which she can use in her PHP request which is a little over-board for this scenario. – Devon Bernard Jan 14 '13 at 03:52
  • @ChayaCooper: So at least now we know a few different solutions we could go with; select field, checkbox, button with javascript query, or button with AJAX and JSON return. Which do you think you would prefer the most for your project? – Devon Bernard Jan 14 '13 at 03:53
  • That's what I've done in the past, and used javascript & css to change the appearance so that the user doesn't see the radio or check box. It just seemed like the input type=button might be a better way to go, but I guess not :-( – Chaya Cooper Jan 14 '13 at 03:54
  • @AfonsoTsukamoto: I'd be happy to share the code for that, if that would help you :-) – Chaya Cooper Jan 14 '13 at 03:55
  • @DevonBernard: If you don't mind updating the answer, I'd love to accept it :-) – Chaya Cooper Jan 14 '13 at 03:56
  • @AfonsoTsukamoto The javascript & css to change the appearance of the radio buttons/check boxes (it's much easier to do it that way) – Chaya Cooper Jan 14 '13 at 04:11
  • @ChayaCooper I would very much like that :) Thanks a lot! How do you exchange code in stack overflow? I'm still a noob here :P – Afonso Tsukamoto Jan 14 '13 at 04:14
  • @AfonsoTsukamoto: It is pretty difficult to share code over StackOverflow. But I think the easiest way would be for her to put her code into a jsfiddle and share the link with you. – Devon Bernard Jan 14 '13 at 04:19
  • @AfonsoTsukamoto: I'll post it below as an answer - that way if someone else is researching this they can benefit from it too :-) I'm still pretty green myself, so I'm thrilled when I can give back to the SO community :-) – Chaya Cooper Jan 14 '13 at 04:20
  • @DevonBernard, yet, another thing to save. Thanks for jsfiddle :) – Afonso Tsukamoto Jan 14 '13 at 04:23
  • @ChayaCooper, once again, thank you very much :) And I get your entusiasm, I feel the same about SO. Been a member only for one month and every time I can answer anything I try my best to answer it wich makes me feel pretty good about it too. There are some real hard problems to solve around, most of them about subjects that I've never even heard about, so being able to help someone is a big deal for me. – Afonso Tsukamoto Jan 14 '13 at 04:26
  • 1
    @AfonsoTsukamoto: I just figured out a really easy way to do this using just html, and I posted the code below :-D – Chaya Cooper Jan 14 '13 at 05:43
1

Since it seems that input type="button" can't capture the user's selection, I wanted to share a really simple way I figured out for doing this with radio buttons or check boxes using only html.

All you need to do is set the input element to style="display:none", and surround both the image and input element with a label tag so that users can click anywhere on the image to select the element :-)

<label for="quiz_start">
<img src="files/start1.jpg" />
<input style="display:none" type="radio" id="quiz_start" name="quiz_start" value="jeans">    
</label>
Chaya Cooper
  • 2,566
  • 2
  • 38
  • 67