Jump to content

Recommended Posts

Hi,

 

I'm trying to make a form in which you can add more textboxes if you need them, I found a page on the internet telling you how to do it and this is what I got:

 

<?php include "/home/a9653716/public_html/header.inc"; ?>
<head>
<script type="text/javascript">

$(document).ready(function(){

    var counter = 2;

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

if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
}   

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

newTextBoxDiv.after().html('<tr><label><td>Textbox #'+ counter + ' : </label></td><td>' +
      '<input type="text" name="textbox' + counter + 
      '" id="textbox' + counter + '" value="" ></td></tr>');

newTextBoxDiv.appendTo("#TextBoxesGroup");


counter++;
     });

     $("#removeButton").click(function () {
if(counter==1){
          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>

</head>
<body>
<table border="0" width="100%"><tr>

<td width="250">
<?php include("/home/a9653716/public_html/adminsidebar.inc"); ?>
</td>
<td valign="top" width="800">

<?php
echo "<div id='content'>";
echo "<h2 div id='title'>";
echo "Create a listing";
echo "</h2>";

//////content//////////////////////////

if(!$_SESSION['uid']){
    header("Location: index.php");
}

$id = mss($_GET['id']);

if ($id) {
    $sql = "SELECT * FROM `item_sub_cats` WHERE `id`='" . $id . "'";
    $res = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($res) == 0) {
        echo "The category you are trying to create a listing on, does not exist!\n";
    } else {
        $row1 = mysql_fetch_assoc($res);

            if (!$_POST['submit']) {
                echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
                echo "<form method=\"post\" action=\"./create_listing.php\">\n";
                echo "<tr><td>Sub Category</td><td><select name=\"cat\">\n";
                $sql2 = "SELECT * FROM `item_cats`";
                $res2 = mysql_query($sql2);
                while ($row = mysql_fetch_assoc($res2)) {
                    $sql3 = "SELECT * FROM `item_sub_cats` WHERE `cid`='" . $row['id'] . "'";
                    $res3 = mysql_query($sql3) or die(mysql_error());

                    echo "<option value=\"0\">" . $row['name'] . "</option>\n";
                    while ($row2 = mysql_fetch_assoc($res3)) {
                        $selected = ($row2['id'] == $id) ? " SELECTED" : "";
                        echo "<option value=\"" . $row2['id'] . "\"" . $selected .
                            ">     " . $row2['name'] . "</option>\n";
                    }
                }
                echo "</select></td></tr>\n";
			echo "<tr><td>Topic Title</td><td><input type=\"text\" name=\"title\"></td></tr>\n";
			?>
                <script>edToolbar('message'); </script>
			<?php
			echo "<tr><td>Message</td><td><textarea id=\"message\" name=\"message\" class=\"ed\"></textarea></td></tr>\n";

			echo "<tr><td><div id='TextBoxesGroup'>";
			echo "<div id='TextBoxDiv1'>";
			echo "<label>Textbox #1 : </label></td><td><input type='textbox' id='textbox1' >";
			echo "</td></tr>";
			echo "</div>";
			echo "</div>";
			echo "<tr><td></td><td><input type='button' value='Add Button' id='addButton'>";
			echo "<input type='button' value='Remove Button' id='removeButton'></td></tr>";					

			echo "<tr><td>Tags (Seperate with commas)</td><td><input type=\"text\" name=\"tags\"></td></tr>\n";
			echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Create Topic\"></td></tr>\n";
                echo "</form></table>\n";
            } else {
                $cat = mss($_POST['cat']);
                $title = mss($_POST['title']);
                $msg = mss($_POST['message']);
			$tags = mss($_POST['tags']);

                if ($cat && $title && $msg && $tags) {
                    $sql = "SELECT * FROM `item_sub_cats` WHERE `id`='" . $cat . "'";
                    $res = mysql_query($sql) or die(mysql_error());
                    if (mysql_num_rows($res) == 0) {
                        echo "This sub category does not exist!\n";
                    } else {
                        $row = mysql_fetch_assoc($res);

                            if (strlen($title) < 3 || strlen($title) > 32) {
                                echo "The title must be between 3 and 32 characters!\n";
                            } else {
                                if (strlen($msg) < 3 || strlen($msg) > 10000) {
                                    echo "The message must be between 3 and 10,000 characters!\n";
                                 } else {
								if (strlen($tags) < 2 || strlen($tags) > 200) {
									echo "You must submit at least 1 tag and the total length mustn't be more than 200 characters!\n";
								} else {
                                    $date = date("m-d-y") . " at " . date("h:i:s");
                                    $time = time();
                                    $sql2 = "INSERT INTO `listing_topics` (`cid`,`title`,`uid`,`date`,`time`,`message`,`tags`) VALUES('" .
                                        $cat . "','" . $title . "','" . $_SESSION['uid'] . "','" . $date . "','" . $time .
                                        "','" . $msg . "','" . $tags . "')";
                                    $res2 = mysql_query($sql2) or die(mysql_error());
                                    $tid = mysql_insert_id();
                                    topic_go($tid);
								}
							}
                            }
                    }
                } else {
            }
        }
}
} else {
    if (!$_POST['submit']) {
        echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
        echo "<form method=\"post\" action=\"./create_listing.php\">\n";
        echo "<tr><td>Sub Category</td><td><select name=\"cat\">\n";
        $sql2 = "SELECT * FROM `item_cats`";
        $res2 = mysql_query($sql2) or die(mysql_error());
        while ($row = mysql_fetch_assoc($res2)) {
            $sql3 = "SELECT * FROM `item_sub_cats` WHERE `cid`='" . $row['id'] . "'";
            $res3 = mysql_query($sql3) or die(mysql_error());

            echo "<option value=\"0\">" . $row['name'] . "</option>\n";
            while ($row2 = mysql_fetch_assoc($res3)) {
                $selected = ($row2['id'] == $id) ? " SELECTED" : "";
                echo "<option value=\"" . $row2['id'] . "\"" . $selected .
                    ">     " . $row2['name'] . "</option>\n";
            }
        }
        echo "</select></td></tr>\n";
        echo "<tr><td>Listing Title</td><td><input type=\"text\" name=\"title\"></td></tr>\n";
	echo "<tr><td>Message</td><td><textarea id=\"markItUp\" name=\"message\" style=\"width:300px;height:100px;\"></textarea></td></tr>\n";
        
	echo "<tr><td><div id='TextBoxesGroup'>";
	echo "<div id='TextBoxDiv1'>";
	echo "<label>Textbox #1 : </label></td><td><input type='textbox' id='textbox1' >";
	echo "</td></tr>";
	echo "</div>";
	echo "</div>";
	echo "<tr><td></td><td><input type='button' value='Add Button' id='addButton'>";
	echo "<input type='button' value='Remove Button' id='removeButton'></td></tr>";


	echo "<tr><td>Tags (Seperate with commas)</td><td><input type=\"text\" name=\"tags\"></td></tr>\n";
	echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Create Topic\"></td></tr>\n";
        echo "</form></table>\n";
    } else {
        $cat = mss($_POST['cat']);
        $title = mss($_POST['title']);
        $msg = mss($_POST['message']);
	$tags = mss($_POST['tags']);

        if ($cat && $title && $msg && $tags) {
            $sql = "SELECT * FROM `item_sub_cats` WHERE `id`='" . $cat . "'";
            $res = mysql_query($sql) or die(mysql_error());
            if (mysql_num_rows($res) == 0) {
                echo "This sub category does not exist!\n";
            } else {
                $row = mysql_fetch_assoc($res);

                    if (strlen($title) < 3 || strlen($title) > 32) {
                        echo "The title must be between 3 and 32 characters!\n";
                    } else {
                        if (strlen($msg) < 3 || strlen($msg) > 10000) {
                            echo "The message must be between 3 and 10,000 characters!\n";
                        } else {
						if (strlen($tags) < 2 || strlen($tags) > 200) {
									echo "You must submit at least 1 tag and no more than a total of 200 characters!\n";
								} else {
                            $date = date("m-d-y") . " at " . date("h:i:s");
                            $time = time();
                            $sql2 = "INSERT INTO `item_listings` (`cid`,`title`,`uid`,`date`,`time`,`message`,`tags`) VALUES
						('" . $cat . "','" . $title . "','" . $_SESSION['uid'] . "','" . $date . "','" . $time . "','" . $msg . "','" . $tags . "')";
                            $res2 = mysql_query($sql2) or die(mysql_error());
                            $tid = mysql_insert_id();
                            header("Location: index.php?act=topic&id=" . $tid . "");
                        }
                    }
			}
            }
        } else {
            echo "Please supply all the fields!\n";
        }
    }
}
?>
</td><td width="250">

<?php include("sidebar.inc"); ?>

</td>
</table>
  </body>

</html>

<?php include("footer.inc"); ?>

 

For some reason I can't figure out how to align them properly, I have attached what I got in the browser,

 

Thank is advanced,

 

coolcam262

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/220572-add-textbox-to-table-in-phpjquery/
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.