Note that there are some explanatory texts on larger screens.

plurals
  1. POWant two dynamically created input values to be saved in one mysql row using php
    text
    copied!<p>what i want is when a user clicks a link it should automatically create <strong>two</strong> text box's at a time and from which we can click and create unlimited numbers of textboxs which when submitted it should save all the dynamically created textbox two text box's in a row.</p> <p>meaning textboxA textboxB</p> <p>in this manner......</p> <p>I found a code on net which works very similar to that how i wanted...but instead of two textboxs it creates only one textbox at a time when clicked the link First i'll give u the full original code...</p> <p>1) index.php</p> <pre><code>&lt;?php //Include the database class require("classes/db.class.php"); ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt; &lt;title&gt;jQuery&lt;/title&gt; &lt;script type="text/javascript" src="js/jquery.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" type="text/css" href="css/css.css" /&gt; &lt;script type="text/javascript"&gt; var count = 0; $(function(){ $('p#add_field').click(function(){ count += 1; $('#container').append( '&lt;strong&gt;Link #' + count + '&lt;/strong&gt;&lt;br /&gt;' + '&lt;input id="field_' + count + '" name="fields[]' + '" type="text" /&gt;&lt;br /&gt;' ); }); }); &lt;/script&gt; &lt;body&gt; &lt;?php //If form was submitted if (isset($_POST['btnSubmit'])) { //create instance of database class $db = new mysqldb(); $db-&gt;select_db(); //Insert static values into users table $sql_user = sprintf("INSERT INTO users (Username, Password) VALUES ('%s','%s')", mysql_real_escape_string($_POST['name']), mysql_real_escape_string($_POST['password']) ); $result_user = $db-&gt;query($sql_user); //Check if user has actually added additional fields to prevent a php error if ($_POST['fields']) { //get last inserted userid $inserted_user_id = $db-&gt;last_insert_id(); //Loop through added fields foreach ( $_POST['fields'] as $key=&gt;$value ) { //Insert into websites table $sql_website = sprintf("INSERT INTO websites (Website_URL) VALUES ('%s')", mysql_real_escape_string($value) ); $result_website = $db-&gt;query($sql_website); $inserted_website_id = $db-&gt;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-&gt;query($sql_users_website); } } else { //No additional fields added by user } echo "&lt;h1&gt;User Added, &lt;strong&gt;" . count($_POST['fields']) . "&lt;/strong&gt; website(s) added for this user!&lt;/h1&gt;"; //disconnect mysql connection $db-&gt;kill(); } ?&gt; &lt;?php if (!isset($_POST['btnSubmit'])) { ?&gt; &lt;h1&gt;New User Signup&lt;/h1&gt; &lt;form name="test" method="post" action=""&gt; &lt;label for="name"&gt;Username:&lt;/label&gt; &lt;input type="text" name="name" id="name" /&gt; &lt;div class="spacer"&gt;&lt;/div&gt; &lt;label for="name"&gt;Password:&lt;/label&gt; &lt;input type="text" name="password" id="password" /&gt; &lt;div class="spacer"&gt;&lt;/div&gt; &lt;div id="container"&gt; &lt;p id="add_field"&gt;&lt;a href="#"&gt;&lt;span&gt;&amp;raquo; Add your favourite links.....&lt;/span&gt;&lt;/a&gt;&lt;/p&gt; &lt;/div&gt; &lt;div class="spacer"&gt;&lt;/div&gt; &lt;input id="go" name="btnSubmit" type="submit" value="Signup" class="btn" /&gt; &lt;/form&gt; &lt;?php } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>2) db.class.php</p> <pre><code>&lt;?php class mysqldb { /* FILL IN YOUR DATABASE DETAILS BEFORE RUNNING THE EXAMPLE */ var $hostname = "localhost"; var $username = "root"; var $password = "mypassword"; var $database = "unlimited"; function db_connect() { $result = mysql_connect($this-&gt;hostname,$this-&gt;username,$this-&gt;password); if (!$result) { echo 'Connection to database server at: '.$this-&gt;hostname.' failed.'; return false; } return $result; } function select_db() { $this-&gt;db_connect(); if (!mysql_select_db($this-&gt;database)) { echo 'Selection of database: '.$this-&gt;database.' failed.'; return false; } } function query($query) { $result = mysql_query($query) or die("Query failed: $query&lt;br&gt;&lt;br&gt;" . mysql_error()); return $result; mysql_free_result($result); } function fetch_array($result) { return mysql_fetch_array($result); } function num_rows($result) { return mysql_num_rows($result); } function last_insert_id() { return mysql_insert_id(); } function kill() { mysql_close(); } } ?&gt; </code></pre> <p>3) css.css</p> <pre><code>html, input {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 0.8em;} body { width: 500px; margin: 50px auto 0 auto; display: block;} h1 { font-size: 1.5em; color: #333; } input { font-size: 0.9em; padding: 5px; border: 1px solid #ccc; margin: 0; display: block;} a { text-decoration: none; color: #666; font-weight: bold; } a:hover { color: #ff0000; } #divTxt { width:400px; padding: 5px; } p a img { border: none; vertical-align: middle; } .spacer {clear: both; height: 10px; } .btn { width: 90px; font-weight: bold; } #container { border: 1px solid #ccc; padding: 2px; } .clear {overflow: hidden;width: 100%; } </code></pre> <p>4) <strong>JQUERY.js</strong></p> <p>With this code i am only allowed to dynamically create one textbox when clicked the link as i said earlier, so to make it for my use as i wanted to have two textbox's i have edited the jquery part in the <code>index.php</code> page as below...</p> <pre><code>&lt;script type="text/javascript"&gt; var count = 0; $(function(){ $('p#add_field').click(function(){ count += 1; $('#container').append( '&lt;strong&gt;Link #' + count + '&lt;/strong&gt;&lt;br /&gt;' + '&lt;label for="fields[]' + '"&gt;Colour&lt;/label&gt;&lt;input id="field_' + count + '" name="fields[]' + '" type="text" /&gt;&lt;label for="fields2[]' + '"&gt;Quantity&lt;/label&gt;&lt;input id="field2_' + count + '" name="fields2[]' + '" type="text" /&gt;&lt;br /&gt;'); }); }); &lt;/script&gt; </code></pre> <p>Till here i am successfull... but the main problem is I cannot save them both the two textbox's in a row in mysql table..</p> <p>Please review this code and reply me if u get any answers.....</p> <p>I'll surely click the green arrow for the working answer as accepted answer..</p> <p>Please HELP guys...... </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload