Post data from array

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok guys, I'm sure that I can figure this out if I spend time working on it; however, I was wondering if maybe somebody would be really, really nice and tell me quickly how to do it. I have a java script that when you click on a button to add a new topic, it creates new text boxes on the page, one with an name of textbx[] and the other with a name of readbx[], so when the data is types into the textboxes, it will put it into an array. How do I pull that data from the array in my action script to post to the database? Here is my code of the form.

Code: [ Select ]
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<script type='text/javascript'>

var inival=0; // Initialise starting element number

// Call this function to add textbox
function addTextBox()
{
var newArea = add_New_Element();
var htcontents = "<input type='text' name='textbx[]' size='50'/> <input type='text' name='readbx[]' size='50'/>";
document.getElementById(newArea).innerHTML = htcontents; // You can any other elements in place of 'htcontents'
}


function add_New_Element() {
inival=inival+1; // Increment element number by 1
var ni = document.getElementById('area');
var newdiv = document.createElement('div'); // Create dynamic element
var divIdName = 'my'+inival+'Div';
newdiv.setAttribute('id',divIdName);
ni.appendChild(newdiv);
return divIdName;
}

</script>
<html>
<head>
<title>SNP Intranet</title>
</head>
<body>
<center>
<form action="topics.php" method="post">
<table width="670">
    <tr>
        <td colspan="2">
            Please list each topic in the white boxes below, separately and in order. List every topic that you intend to discuss in your lecture and you may use as many as needed. For each new topic, click the "Add New Topic" button and a new line will appear in which you can add your next topic. Next to the topic, list the chapter(s) or the text book where this topic is most prominent. In addition, you may suggest an appendix from the text or an article from an outside source that the student should read in addition to the chapter reading assignment that will further their knowledge on the topic.<p />Use the topics below to write your script and/or prepare your lecture notes that you will use in the recording phase. <br /><strong>(Limit 20 Topics)</strong>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <hr />
        </td>
    </tr>
    <tr>
        <td colspan="2">Course title: <input type="text" name="c_name" size="93" /></td>
    </tr>
    <tr>
     <td width="300">Topics</td>
     <td>Text Chapters, Articles, and Appendices</td>
    </tr>
        <tr>
        <td colspan="2"><div id='area'></div></td>
    </tr>
    <tr>
        <td><input type="button" name="add" onclick="addTextBox()" value="Add New Topic"></td>
    </tr>

</table>
<input type="submit" name="submit" value="Update" /> <input type="button" name="cancel" id="cancel" value="Cancel" onclick="history.back()" />
</form>
</body>
</html>
<?php
    if (isset($connection)) {
        mssql_close($connection);
    }
?>
    
  1. <?php require_once("includes/connection.php"); ?>
  2. <?php require_once("includes/functions.php"); ?>
  3. <script type='text/javascript'>
  4. var inival=0; // Initialise starting element number
  5. // Call this function to add textbox
  6. function addTextBox()
  7. {
  8. var newArea = add_New_Element();
  9. var htcontents = "<input type='text' name='textbx[]' size='50'/> <input type='text' name='readbx[]' size='50'/>";
  10. document.getElementById(newArea).innerHTML = htcontents; // You can any other elements in place of 'htcontents'
  11. }
  12. function add_New_Element() {
  13. inival=inival+1; // Increment element number by 1
  14. var ni = document.getElementById('area');
  15. var newdiv = document.createElement('div'); // Create dynamic element
  16. var divIdName = 'my'+inival+'Div';
  17. newdiv.setAttribute('id',divIdName);
  18. ni.appendChild(newdiv);
  19. return divIdName;
  20. }
  21. </script>
  22. <html>
  23. <head>
  24. <title>SNP Intranet</title>
  25. </head>
  26. <body>
  27. <center>
  28. <form action="topics.php" method="post">
  29. <table width="670">
  30.     <tr>
  31.         <td colspan="2">
  32.             Please list each topic in the white boxes below, separately and in order. List every topic that you intend to discuss in your lecture and you may use as many as needed. For each new topic, click the "Add New Topic" button and a new line will appear in which you can add your next topic. Next to the topic, list the chapter(s) or the text book where this topic is most prominent. In addition, you may suggest an appendix from the text or an article from an outside source that the student should read in addition to the chapter reading assignment that will further their knowledge on the topic.<p />Use the topics below to write your script and/or prepare your lecture notes that you will use in the recording phase. <br /><strong>(Limit 20 Topics)</strong>
  33.         </td>
  34.     </tr>
  35.     <tr>
  36.         <td colspan="2">
  37.             <hr />
  38.         </td>
  39.     </tr>
  40.     <tr>
  41.         <td colspan="2">Course title: <input type="text" name="c_name" size="93" /></td>
  42.     </tr>
  43.     <tr>
  44.      <td width="300">Topics</td>
  45.      <td>Text Chapters, Articles, and Appendices</td>
  46.     </tr>
  47.         <tr>
  48.         <td colspan="2"><div id='area'></div></td>
  49.     </tr>
  50.     <tr>
  51.         <td><input type="button" name="add" onclick="addTextBox()" value="Add New Topic"></td>
  52.     </tr>
  53. </table>
  54. <input type="submit" name="submit" value="Update" /> <input type="button" name="cancel" id="cancel" value="Cancel" onclick="history.back()" />
  55. </form>
  56. </body>
  57. </html>
  58. <?php
  59.     if (isset($connection)) {
  60.         mssql_close($connection);
  61.     }
  62. ?>
  63.     
  • yomi
  • Graduate
  • Graduate
  • User avatar
  • Posts: 116
  • Loc: Nigeria

Post 3+ Months Ago

hi, I'm not sure i understand your requirement. What i can make of it is that you want to collect a set of values you want to store in the same field in your databse. If so, I have something like that but i did so with checkboxes.

On my form i have :

Code: [ Select ]
<input type="checkbox" value="saved money" name="checkbox[]">Company</label><br />
<label><input type="checkbox" value="money" name="checkbox[]"/>Money</label><br />
<label><input type="checkbox" value="business" name="checkbox[]"/>Business</label> <br />
<label><input type="checkbox" value="ambassador" name="checkbox[]"/>Ambassador</label><br />
<label><input type="checkbox" value="customer" name="checkbox[]"/>Customer </label>
  1. <input type="checkbox" value="saved money" name="checkbox[]">Company</label><br />
  2. <label><input type="checkbox" value="money" name="checkbox[]"/>Money</label><br />
  3. <label><input type="checkbox" value="business" name="checkbox[]"/>Business</label> <br />
  4. <label><input type="checkbox" value="ambassador" name="checkbox[]"/>Ambassador</label><br />
  5. <label><input type="checkbox" value="customer" name="checkbox[]"/>Customer </label>


then in my script :

Code: [ Select ]
if (isset($_POST['checkbox']) ) {
$_POST['checkbox'] = implode(', ', $_POST['checkbox']); }

$text_chptrs = $_POST ['checkbox'];
  1. if (isset($_POST['checkbox']) ) {
  2. $_POST['checkbox'] = implode(', ', $_POST['checkbox']); }
  3. $text_chptrs = $_POST ['checkbox'];


Then you can read $text_chptrs into your database with an sql
Moderator Remark: Added [code] tags
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

That works great, with one exception. That puts everything in my first array in one field a and everything in my second array in another field. What I need is for each postion to be in a different field.

What I mean is, I have a table that has ID, textbx, and readbx fields. So, if using the form that I have to generate the new topics, when somebody post something in the first topic, its going to post in array location 0, so in row 1 in my database, I need textbx[0] to post in the textbx field and readbx[0] to post in the readbx field. Then, if the person addes another topic, meaning that I now have something in position 0 and position 1 of my array, I need the textbx[0] and readbx[0] to post in textbx field and readbx field in the same database row, but then I also need textbx[1] and readbx[1] to post in the textbx and readbx field of the next row. So, basically I'm wanting to split the positions of the arrays, in rows in the database. I know its possible, I'm just having a hard time figuring it out.

Thanks for any help you can offer!!!

Jordon
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

This is about the closest that I've got so far with it. I'm trying to run a loop through my array and post in a new row for each position. I'm only dealing with one array at this time and only trying to post in the tp_topic field. Everytime that I test this with more than one topics, meaning more than one thing in my array, I get an error and it only post the first thing in my array in my database. Here is the code:

Code: [ Select ]
foreach($topics as $topic) {
    $query = "INSERT INTO SNP_topics (tp_topic)
        VALUES ('$topic')";
            $results1 = mssql_query($query, $connection);
            if ($results) {
            } else {
            die("Database query failed: " . mssql_error());
            }
}
  1. foreach($topics as $topic) {
  2.     $query = "INSERT INTO SNP_topics (tp_topic)
  3.         VALUES ('$topic')";
  4.             $results1 = mssql_query($query, $connection);
  5.             if ($results) {
  6.             } else {
  7.             die("Database query failed: " . mssql_error());
  8.             }
  9. }
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Nevermind, thanks for all your help, I figured it out. My results varible was wrong, in one place I was calling it results1 and in another place, just results. Once I fixed that, it worked like a charm!!!!
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, I'm just having a complete brain freeze here. I'm running a loop that will submit my topic to the field tp_topic and it doesn't matter how many topics I have, it will always do that; however, i want it also in the same database row to submit the read to the tp_read field. Everytime I try to run two loops on both array, the textbx array and the readbx array, I end up with the data posting twice, becuase its running two loops; however, I can't figure out how to get it to only post one record for both the textbx and the readbx that are in the same array position. So, if I have Science in textbx[0] and I have Chapter 1 in readbx[0], I want both of them to be in the same record in the database for tp_topic and tp_read fields. Here is my input boxes:

Code: [ Select ]
<input type='text' name='textbx[]' size='49'/> <input type='text' name='readbx[]' size='49'/>


And so far, here is my action script:

Code: [ Select ]
if (isset($_POST['submit'])){
     $topics = $_POST['textbx'];
     $reads = $_POST['readbx'];


foreach($topics as $topic) {
    $query = "INSERT INTO SNP_topics (tp_topic, tp_read)
        VALUES ('$topic', '$read')";
            $results = mssql_query($query, $connection);
            if ($results) {
            } else {
            die("Database query failed: " . mssql_error());
            }
}
    }
  1. if (isset($_POST['submit'])){
  2.      $topics = $_POST['textbx'];
  3.      $reads = $_POST['readbx'];
  4. foreach($topics as $topic) {
  5.     $query = "INSERT INTO SNP_topics (tp_topic, tp_read)
  6.         VALUES ('$topic', '$read')";
  7.             $results = mssql_query($query, $connection);
  8.             if ($results) {
  9.             } else {
  10.             die("Database query failed: " . mssql_error());
  11.             }
  12. }
  13.     }



Any suggestions????

Thanks!!!

Jordon
  • Dorky
  • Novice
  • Novice
  • Dorky
  • Posts: 15

Post 3+ Months Ago

ok well i am having some issues removing the slashes from the first loop in this but i think the first loop does something like you are looking for. i have some extra code for feedback on my variables so you will want to keep that in mind when reviewing this.

Code: [ Select ]
<?php

$pspell_link = pspell_new("en");
if(isset($_GET['text'])) {
$words = stripslashes($_GET['text']) ; }
echo "<form method='get' action='spell.php'>";


if (isset($_GET['0'])) echo "0";
{
foreach($proces as $key0 => $marker) 
{
$value = split('[ ]', $marker ); echo "Value : |0| $value[0] |1| $value[1] |Key| $key0 <hr>";
$value0[] = $value[0] ; $value1[] = $value[1] ;
echo "Print Value Stage 1:"; print_r($value0); print_r($value1); echo "<hr>";
}
$print = str_replace( $value0 , $value1 , $words );

}


if(!isset($_GET['0']))
{
$print = $words;
}







if (isset($_GET['check']))
{ echo "<b><i> $words </i></b><hr>";
$string = split('[ ]', $words); foreach($string as $key1 => $word)
if (!pspell_check($pspell_link, $word))
{
echo "<hr><b><i>$word</i></b><br>"; $suggestions = pspell_suggest($pspell_link, $word);
foreach($suggestions as $key2 => $suggestion)
{
    $send = "$word $suggestion"; echo "<input type='checkbox' name='$key1' value='$send'>$suggestion<br>";
}
}
}


echo "<hr><textarea name='text'class='textarea' type='text/plain' rows='15' cols='40'>$print</textarea><hr>";
if(!isset($_GET['check'])) { echo "<button name='check' value='check check' type='submit'>Check Spelling</button>"; }
if(isset($_GET['check'])) { echo "<button name='done' value='done done' type='submit'>Done</button>"; }
echo "</form>";

unset($print);

?>
  1. <?php
  2. $pspell_link = pspell_new("en");
  3. if(isset($_GET['text'])) {
  4. $words = stripslashes($_GET['text']) ; }
  5. echo "<form method='get' action='spell.php'>";
  6. if (isset($_GET['0'])) echo "0";
  7. {
  8. foreach($proces as $key0 => $marker) 
  9. {
  10. $value = split('[ ]', $marker ); echo "Value : |0| $value[0] |1| $value[1] |Key| $key0 <hr>";
  11. $value0[] = $value[0] ; $value1[] = $value[1] ;
  12. echo "Print Value Stage 1:"; print_r($value0); print_r($value1); echo "<hr>";
  13. }
  14. $print = str_replace( $value0 , $value1 , $words );
  15. }
  16. if(!isset($_GET['0']))
  17. {
  18. $print = $words;
  19. }
  20. if (isset($_GET['check']))
  21. { echo "<b><i> $words </i></b><hr>";
  22. $string = split('[ ]', $words); foreach($string as $key1 => $word)
  23. if (!pspell_check($pspell_link, $word))
  24. {
  25. echo "<hr><b><i>$word</i></b><br>"; $suggestions = pspell_suggest($pspell_link, $word);
  26. foreach($suggestions as $key2 => $suggestion)
  27. {
  28.     $send = "$word $suggestion"; echo "<input type='checkbox' name='$key1' value='$send'>$suggestion<br>";
  29. }
  30. }
  31. }
  32. echo "<hr><textarea name='text'class='textarea' type='text/plain' rows='15' cols='40'>$print</textarea><hr>";
  33. if(!isset($_GET['check'])) { echo "<button name='check' value='check check' type='submit'>Check Spelling</button>"; }
  34. if(isset($_GET['check'])) { echo "<button name='done' value='done done' type='submit'>Done</button>"; }
  35. echo "</form>";
  36. unset($print);
  37. ?>
Moderator Remark: Added [code] tags
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Thank you all for your help!!! I ended up figuring out the solution and wanted to share, in case somebody else was having the same problem. Basically, I added a field in my database named tp_num. I then set to variables at the top of my script = to 0. I then ran two loops, the first loop for the textbx array. At the begining of my loop, i set my varible that was =0 to add each time it looped. I then set my query to insert the varible to the tp_num field and the data from the array into the tp_topic field. I then ran a second loop that added to my other varible that was = 0, each time it looped and instead of doing an insert, I did a update to the table, where the two tp_num variables where the same. Basically, the script looked like this:

Code: [ Select ]
$topicnm = 0;
$readnm = 0;

foreach($topics as $topic) {
    $topicnm++;
    $query2 = "INSERT INTO SNP_topics (tp_num, tp_topic)
        VALUES ('$topicnm', '$topic')";
            $results2 = mssql_query($query2, $connection);
            if ($results2) {
            } else {
            die("Database query failed: " . mssql_error());
            }
        }

foreach($reads as $read) {
    $readnm++; 
    $query3 = "UPDATE SNP_TOPICS SET
                tp_read = '{$read}'
            WHERE tp_num = '{$readnm}'";
            $results3 = mssql_query($query3, $connection);
            if ($results3) {
            } else {
            die("Database query failed: " . mssql_error());
            }
    }
  1. $topicnm = 0;
  2. $readnm = 0;
  3. foreach($topics as $topic) {
  4.     $topicnm++;
  5.     $query2 = "INSERT INTO SNP_topics (tp_num, tp_topic)
  6.         VALUES ('$topicnm', '$topic')";
  7.             $results2 = mssql_query($query2, $connection);
  8.             if ($results2) {
  9.             } else {
  10.             die("Database query failed: " . mssql_error());
  11.             }
  12.         }
  13. foreach($reads as $read) {
  14.     $readnm++; 
  15.     $query3 = "UPDATE SNP_TOPICS SET
  16.                 tp_read = '{$read}'
  17.             WHERE tp_num = '{$readnm}'";
  18.             $results3 = mssql_query($query3, $connection);
  19.             if ($results3) {
  20.             } else {
  21.             die("Database query failed: " . mssql_error());
  22.             }
  23.     }


I don't know if this was the ideal solution, but it works. Bascially, I'm able to post the data from two array, and keeping the data from those arrays that are in the same position, in the same record of the database.

I spent so much time on this that I almost shed a tear when it actually worked!!! :lol:

Once again, thanks for all the help!!!

Jordon
  • yomi
  • Graduate
  • Graduate
  • User avatar
  • Posts: 116
  • Loc: Nigeria

Post 3+ Months Ago

great :idea: ! congrats!
  • Dorky
  • Novice
  • Novice
  • Dorky
  • Posts: 15

Post 3+ Months Ago

no prob dude. glad i could help. whats even more amazing is i have only been into php by about 3 weeks. lol.

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 131 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.