Jump to content

submit jQuery dynamic names to Mysqldb


Thomas1_86

Recommended Posts

So I am trying to submit jQuery dynamic name Qty and Field into one insert. With the code below It Submits like this example qty1 with field1, qty1 with field2, gty2 with field1, and qty2 with field2. When what it need to do is qty1 with field1 and qty2 with field2. If this help here is some of dynamic text box code

 

    newTextBoxDiv.after().html('<label>QTY '+ counter + ' : </label>' +
          '<input type="text" size="5" name="Qtys[]' +
          '" id="Qty_' + counter + '" value="" > <label>Ingredient '+ counter + ' : </label>' +
          '<input type="text" name="fields[]' + 
          '" id="field_' + counter + '" value="" >');

 


<?php
//If form was submitted
if (isset($_POST['btnSubmit'])) {

//create instance of database class
$db = new mysqldb();
$db->select_db();

//Insert static values into users table
$sql_user = sprintf("INSERT INTO recipe (Recipe, Description) VALUES ('%s', '%s')",
					mysql_real_escape_string($_POST['name']),
                        mysql_real_escape_string($_POST['Description']) );  
$result_user = $db->query($sql_user);


//Check if user has actually added additional fields to prevent a php error
    if ($_POST['Qtys'])
if ($_POST['fields']) {

	//get last inserted userid
	$inserted_user_id = $db->last_insert_id();

	//Loop through added fields
        foreach ( $_POST['Qtys'] as $key=>$Qty )
	foreach ( $_POST['fields'] as $key=>$value )  {

		//Insert into websites table
		$sql_website = sprintf("INSERT INTO ingredient (Qty, Field) VALUES ('%s', '%s')",
                                   mysql_real_escape_string($Qty),
					    	   mysql_real_escape_string($value) );  
		$result_website = $db->query($sql_website);
		$inserted_website_id = $db->last_insert_id();


		//Insert into users_websites_link table
		$sql_users_website = sprintf("INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')",
					    	   mysql_real_escape_string($inserted_user_id),
							   mysql_real_escape_string($inserted_website_id) );  
		$result_users_website = $db->query($sql_users_website);

	}

} else {

	//No additional fields added by user

}
echo "<h1>Recipe Added, <strong>" . count($_POST['fields']) . "</strong> Ingredient in this recipe!</h1>";

//disconnect mysql connection
$db->kill();
}
?>


Link to comment
Share on other sites

I hope this helps. There are no Errors. I have Tried To post the Array directly << which is not correct. And the way posted above, which is posting qty 2 with Fields[] and Qtys[] with ing2 and this is not what i was trying to achieve. I want the first array qty and first Ingredient to post together with a unique WebsiteID so I can call my recipe back up like the recipe reads on my paper. 

post-129863-13482403114128_thumb.jpg

Link to comment
Share on other sites

I put Comments in where I feel I'm getting the wrong arrays posting with each other in the ingredients table. I tried to map out my image this time. Hope this helps.

 

<?php 
//Include the database class
require("classes/db.class.php");
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Add New Recipe</title>
<script type="text/javascript" src="js/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="css/css.css" />

</head>


<script type="text/javascript">

$(document).ready(function(){

    var counter = 1;

    $("#addButton").click(function () {

    if(counter>20){
            alert("Max 20 Ingredients");
            return false;
    }   

    var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);

    newTextBoxDiv.after().html('<label>QTY '+ counter + ' : </label>' +
          '<input type="text" size="5" name="Qtys[]' + 
          '" id="Qty_' + counter + '" value="" > <label>Ingredient '+ counter + ' : </label>' +
          '<input type="text" name="fields[]' + 
          '" id="field_' + counter + '" value="" >');

    newTextBoxDiv.appendTo("#TextBoxesGroup");


    counter++;
     });

     $("#removeButton").click(function () {
         // switch counter to 1 to enable alert
    if(counter==0){
          alert("No more textbox to remove");
          return false;
       }   

    counter--;

        $("#TextBoxDiv" + counter).remove();

     });

     $("#getButtonValue").click(function () {

    var msg = '';
    for(i=1; i<counter; i++){
         msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
    }
          alert(msg);
     });
  });
</script>




<body>

<?php
//If form was submitted
if (isset($_POST['btnSubmit'])) {

//create instance of database class
$db = new mysqldb();
$db->select_db();

//Insert static values into users table
$sql_user = sprintf("INSERT INTO recipe (Recipe, Description) VALUES ('%s', '%s')",
					mysql_real_escape_string($_POST['name']),
                        mysql_real_escape_string($_POST['Description']) );  
$result_user = $db->query($sql_user);


//Check if user has actually added additional fields to prevent a php error
    if ($_POST['Qtys'])              // This Is where I think I am getting the problem or the wrong values getting submitted along with the right ones
if ($_POST['fields']) {     // This Is where I think I am getting the problem or the wrong values getting submitted along with the right ones

	//get last inserted userid
	$inserted_user_id = $db->last_insert_id();

	//Loop through added fields
        foreach ( $_POST['Qtys'] as $key=>$Qty )
	foreach ( $_POST['fields'] as $key=>$value )  {

		//Insert into websites table
		$sql_website = sprintf("INSERT INTO ingredient (Qty, Website_URL) VALUES ('%s', '%s')",
                                   mysql_real_escape_string($Qty),           // This Is where I think I am getting the problem or the wrong values getting submitted along with the right ones
		           mysql_real_escape_string($value) );     // This Is where I think I am getting the problem or the wrong values getting submitted along with the right ones
		$result_website = $db->query($sql_website);
		$inserted_website_id = $db->last_insert_id();


		//Insert into users_websites_link table
		$sql_users_website = sprintf("INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')",
					    	   mysql_real_escape_string($inserted_user_id),
							   mysql_real_escape_string($inserted_website_id) );  
		$result_users_website = $db->query($sql_users_website);

	}

} else {

	//No additional fields added by user

}
echo "<h1>Recipe Added, <strong>" . count($_POST['fields']) . "</strong> Ingredient in this recipe!</h1>";

//disconnect mysql connection
$db->kill();
}
?>

<?php if (!isset($_POST['btnSubmit'])) { ?>
    <div align="center">
                <h1>New Recipe</h1>

                <form name="test" method="post" action="">

                <div class="alignCenter">
                    <label class="alignTopLeft" for="name" >         Recipe:</label>
                    <input type="text" name="name" id="name" />
                </div>

                <br>
                <br>



                <div id='TextBoxesGroup'>


                    <div id="TextBoxDiv">
                    </div>
                    <br>
                </div>

                <br>
                <br>

                <div class="alignCenter">
                    <input type='button' value='+ Ingredient' id='addButton' >
                    <input type='button' value='- Ingredient' id='removeButton' >
                </div>

                <br>
                <br>

                <div >
                    <label class="alignTopLeft" for="name">Instructions:</label>
                    <textarea rows="15" cols="50" name="Description" id="Description" /></textarea> 
                </div> 
                <br>                                                
                <br>
                <div>
                    <input id="go" name="btnSubmit" type="submit" value="Submit Recipe" class="btn" />
                </div>
            </div>
            </form>
<?php } ?>

</body>
</html>

post-129863-13482403118784_thumb.jpg

Link to comment
Share on other sites

It works great.

 

<?php 
//Include the database class
require("classes/db.class.php");
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Add New Recipe</title>
<script type="text/javascript" src="js/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="css/css.css" />

</head>


<script type="text/javascript">

$(document).ready(function(){

    var counter = 1;

    $("#addButton").click(function () {

    if(counter>20){
            alert("Max 20 Ingredients");
            return false;
    }   

    var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);

    newTextBoxDiv.after().html('<label>QTY '+ counter + ' : </label>' +
          '<input type="text" size="5" name="Qtys[]' + 
          '" id="Qty_' + counter + '" value="" > <label>Ingredient '+ counter + ' : </label>' +
          '<input type="text" name="fields[]' + 
          '" id="field_' + counter + '" value="" >');

    newTextBoxDiv.appendTo("#TextBoxesGroup");


    counter++;
     });

     $("#removeButton").click(function () {
         // switch counter to 1 to enable alert
    if(counter==0){
          alert("No more textbox to remove");
          return false;
       }   

    counter--;

        $("#TextBoxDiv" + counter).remove();

     });

     $("#getButtonValue").click(function () {

    var msg = '';
    for(i=0; i<counter; i++){
         msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
    }
          alert(msg);
     });
  });
</script>

<body>
<?php
//If form was submitted
if (isset($_POST['btnSubmit'])) {

    //create instance of database class
    $db = new mysqldb();
    $db->select_db();
                    
    //Insert static values into users table
         $sql_user = sprintf("INSERT INTO recipe (Recipe, Description) VALUES ('%s', '%s')", 
//   $sql_user = sprintf("INSERT INTO users (Username, Password) VALUES ('%s','%s')",
                        mysql_real_escape_string($_POST['name']),
                        mysql_real_escape_string($_POST['Description']) );  
    $result_user = $db->query($sql_user);

     $inserted_user_id = $db->last_insert_id();
             
            if (isset($_POST['Qtys'])){   
                for ( $i=0;$i<count($_POST['Qtys']);$i++) {
                    $Qtys = $_POST['Qtys'][$i];
                    $fields = $_POST['fields'][$i];                                                
            //Insert into websites table
            $sql_website = sprintf("INSERT INTO ingredient (Qty, Website_URL) VALUES ('%s', '%s')",
                                   mysql_real_escape_string($Qtys), 
                                   mysql_real_escape_string($fields) );  
            $result_website = $db->query($sql_website);
            $inserted_website_id = $db->last_insert_id();           
                       //Insert into users_websites_link table
            $sql_users_website = sprintf("INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')",
                                   mysql_real_escape_string($inserted_user_id),
                                   mysql_real_escape_string($inserted_website_id) );  
            $result_users_website = $db->query($sql_users_website);            
        }
               } else {
    
        //No additional fields added by user        
    }
    echo "<h1>User Added, <strong>" . count($_POST['Qtys']) . "</strong> qtys(s) added for this user!</h1>";
        //disconnect mysql connection
    $db->kill();
}
?>
<?php if (!isset($_POST['btnSubmit'])) { ?>
        <div align="center">
            <h1>New Recipe</h1>

            <form name="test" method="post" action="">

            <div class="alignCenter">
                <label class="alignTopLeft" for="name" >         Recipe:</label>
                <input type="text" name="name" id="name" />
            </div>

            <br>
            <br>



            <div id='TextBoxesGroup'>


                <div id="TextBoxDiv">
                </div>
                <br>
            </div>

            <br>
            <br>

            <div class="alignCenter">
                <input type='button' value='+ Ingredient' id='addButton' >
                <input type='button' value='- Ingredient' id='removeButton' >
            </div>

            <br>
            <br>

            <div >
                <label class="alignTopLeft" for="name">Instructions:</label>
                <textarea rows="15" cols="50" name="Description" id="Description" /></textarea> 
            </div> 
            <br>                                                
            <br>
            <div>
                <input id="go" name="btnSubmit" type="submit" value="Submit Recipe" class="btn" />
            </div>
        </div>
        </form>
<?php } ?>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.