5 Replies - 468 Views - Last Post: 15 August 2012 - 04:41 AM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

How do I save this to Database?

Posted 15 August 2012 - 12:31 AM

I have form A which (after some checkboxes checked) populates form B, see here: http://methinks.info/testform1.php

When the button is clicked, I can collect checkboxes checked on form B, but how do I collect those checked on form A (which does not have a submit button)?

thanks

This post has been edited by g37752: 15 August 2012 - 12:31 AM

Is This A Good Question/Topic? 0
  • +

Replies To: How do I save this to Database?

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How do I save this to Database?

Posted 15 August 2012 - 01:10 AM

Insert it into a database?

What database do you have? Do you already have a connection made? What have you tried already?
Was This Post Helpful? 0
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: How do I save this to Database?

Posted 15 August 2012 - 01:13 AM

yes - i am able to save the 2nd form into a mysql database.
the problem is the 1st form does not have a submit button.
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How do I save this to Database?

Posted 15 August 2012 - 03:30 AM

Well you have 2 options: Add a submit button, or, upon a checkbox being ticked, fire off an AJAX request to save the checkbox tick into the database.
Was This Post Helpful? 0
  • +
  • -

#5 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: How do I save this to Database?

Posted 15 August 2012 - 04:22 AM

I guess ajax is my only option now.

now i cannot save to database when user is still making clicks. So what should happen is that when user clicks the button on the 2nd form, i save everything to database. When user is clicking the 1st form, i am already recording the selection in a <span> element on the 2nd form. according to my understanding now, ajax is the only way to collect non-<input>-element's values?

can you help me a bit with the code? here is what I come up with so far, but I couldn't get it to write to the DB: http://www.dreaminco...ed/page__st__15
Was This Post Helpful? 0
  • +
  • -

#6 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: How do I save this to Database?

Posted 15 August 2012 - 04:41 AM

here is the code i have now:

<form>
   <table cellspacing=5 cellpadding=5 border=1>
   <tr><td id="cell2_1" style="display:none;">      (turns display:inline after user action)
   <input name="ckbox[]" type="checkbox" id="trn2_1"  value="trn2_1 onclick="chooseBets(this.form)">2=1</input>
   </td><td id="cell3_1" style="display:none;">
   <input name="ckbox[]" type="checkbox" id="trn3_1"  value="trn3_1 onclick="chooseBets(this.form)">3=1</input>
   </td></tr>
   <tr><td colspan=5>selected from 1st form: <span id="list-selected"></span></td></tr>
   <tr><td colspan=5># of games selected: <b id="numb">0</b></td></tr>
   <tr><td colspan=5>selected from 2nd form: <span id="tron-selected"></span></td></tr>
   <tr><td colspan=5><input type="submit" name="btnOrder" id="btnOrder" value="place order"/></td></tr>
   </table>
</form>


<script type="text/javascript">
    $("#btnOrder").click( function(){

		var $form = $(this);
		serializedData = $form.serialize();
		echo 'serialized: '.serializedData;
    $.ajax({
        url: "save2tdb.php",
        type: "post",
        data: serializedData,
        success: function(response, textStatus, jqXHR){
            // log a message to the console
            console.log("Hooray, it worked!");
        },
        error: function(jqXHR, textStatus, errorThrown){
            // log the error to the console
            console.log(
                "The following error occured: "+
                textStatus, errorThrown
            );
        },
        complete: function(){
            // enable the inputs
            //$inputs.removeAttr("disabled");
        }
    });
	  
	});

</script>


save2tdb.php:

<?php
  ..........
  $bets_selected="";
  foreach($_POST as $key => $value) {
    if(strstr($key, "ckbox")) {           
      $num_bets = count($value);
      for ($i=0;$i<$num_bets;$1++)
      {  
         $bets_selected = $value[i].', '.$bets_selected;
      } 
      break;
      }
    }
  $sql = "INSERT INTO orders (order_date,bets_selected,total_bets) VALUES ('2012-08-13','$bets_selected','$num_bets')";
  .......
?>


My main thing is to save the <span> elements.. but I have a hard time even saving the checkboxes now..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1