|
-
Nov 26th, 2006, 06:53 PM
#1
Thread Starter
Hyperactive Member
Passing more than one textarea variable?
Ok I have a form which has x number of checkboxes and textareas as this:
Code:
echo "<input name='skills[]' type='checkbox' onclick='this.form.txt$skillname.disabled = !this.checked' value='$skillid' checked>$skillname: <textarea rows='3' cols='20' name=txt$skillname></textarea><br />";
Now I have to pass these form variables to postme.php
I know how to pass the checkbox variables --
Code:
$skills=$_POST['skills'];
As that passes an array of checkbox values...
But how would I pass the textarea ones? They each have a different name such as txtOne, txtTwo etc... so how can I pass them as I need to be able to access them without knowin the name...?
-
Nov 26th, 2006, 07:29 PM
#2
Stuck in the 80s
Re: Passing more than one textarea variable?
Code:
<textarea name="skills[<?php echo $skillname; ?>]"></textarea>
Then after the submit:
PHP Code:
foreach( $_POST[ "skills" ] as $skillname => $value )
{
}
-
Nov 26th, 2006, 07:44 PM
#3
Thread Starter
Hyperactive Member
Re: Passing more than one textarea variable?
Can you just explain what that code is actually saying in plain english please so I understand it better? Also I use $_POST to get the variables from the form, don't I have to do the same for the textarea variables??
-
Nov 26th, 2006, 07:49 PM
#4
Stuck in the 80s
Re: Passing more than one textarea variable?
Yes, you have to use $_POST to get the values, but you don't have to assign it to a variable like you are doing. That is optional.
The code I posted is a little confusing as it uses the same name as your check boxes. Consider this:
Code:
<textarea name="txtSkills[<?php echo $skillname; ?>]"></textarea>
Then in PHP:
PHP Code:
$txtSkills = $_POST[ "txtSkills" ];
foreach( $txtSkills as $skillname => $value )
{
// $skillname = name of the skill
// $value = value of the textarea for $skillname
}
Pretty much what you are doing is creating an an array in the HTML form (same as you are doing for the check boxes). So each textarea has the name txtSkills, but the index for each is the skillname.
Then in PHP after the form is submitted, you can look txtSkills using foreach() to get the skillname and the value entered in the textarea.
-
Nov 26th, 2006, 08:00 PM
#5
Thread Starter
Hyperactive Member
Re: Passing more than one textarea variable?
Thanks for that it makes more sense now!
Well what I will be doing is to say, if a checkbox $skillname is checked, get the value of txtSkill$skillname... I think thats right at least...
Code:
if( isset($skillname)) // if none are checked, will be false
{
foreach($txtSkills as $skillname => $value)
{
$query = INSERT INTO wherever
mysql_query($query);
}
}
Is that right??? So in other words what I would do is for each skill that is checked I would save its name and value into a table...
-
Nov 26th, 2006, 08:05 PM
#6
Stuck in the 80s
Re: Passing more than one textarea variable?
Something like:
PHP Code:
$skillname = $_POST[ "skills" ];
$txtSkills = $_POST[ "txtSkills" ];
if( isset($skillname)) // if none are checked, will be false
{
foreach($skillname as $value) // loop each checked box
{
// echo the textarea that corresponds to the checkbox
echo $txtSkills[ $skillname ];
}
}
-
Nov 27th, 2006, 08:59 AM
#7
Thread Starter
Hyperactive Member
Re: Passing more than one textarea variable?
This is what I did:
Code:
$skills=$_POST['skills'];
$txt = $_POST[ "txt" ];
Code:
// sorts out the skillsrequired
$query="SELECT Postid FROM Posts WHERE Userid='$user' && Title='$title'";
$result=mysql_query($query) or die( "No Postid");
$postid=mysql_result($result,$i,"Postid");
if( isset($skills)) // if none are checked, will be false
{
foreach($skills as $skillid) // loop each checked box
{
$query = "INSERT INTO Skillsrequired VALUES ('', '$postid','$skillid', '$txt[$skills]')";
mysql_query($query);
}
}
This isn't working.... the skillid is saving but not the comment...
-
Nov 27th, 2006, 10:17 AM
#8
Re: Passing more than one textarea variable?
post what you're using on the form to make the textarea's names.
-
Nov 27th, 2006, 11:14 AM
#9
Thread Starter
Hyperactive Member
Re: Passing more than one textarea variable?
echo "<input name='skills[]' type='checkbox' onclick='this.form.txt$skillname.disabled = !this.checked' value='$skillid' checked>$skillname: <textarea rows='3' cols='20' name=txt$skillname></textarea><br />";
-
Nov 27th, 2006, 11:41 AM
#10
Stuck in the 80s
Re: Passing more than one textarea variable?
Code:
foreach($skills as $skillid) // loop each checked box
{
$query = "INSERT INTO Skillsrequired VALUES ('', '$postid','$skillid', '$txt[$skills]')";
mysql_query($query);
}
Should be:
Code:
foreach($skills as $skillid) // loop each checked box
{
$query = "INSERT INTO Skillsrequired VALUES ('', '$postid','$skillid', '$txt[$skillid]')";
mysql_query($query);
}
-
Nov 27th, 2006, 11:42 AM
#11
Stuck in the 80s
Re: Passing more than one textarea variable?
 Originally Posted by wwwfilmfilercom
echo "<input name='skills[]' type='checkbox' onclick='this.form.txt$skillname.disabled = !this.checked' value='$skillid' checked>$skillname: <textarea rows='3' cols='20' name=txt$skillname></textarea><br />";
Should be:
Code:
<textarea rows='3' cols='20' name='txt[$skillname]'></textarea>
-
Nov 27th, 2006, 11:58 AM
#12
Thread Starter
Hyperactive Member
Re: Passing more than one textarea variable?
Hmmm doesn't seem to work - what exactly is that line saying???
For each checkbox in the $skills array, get $skillid {
put '', postid, skillid, and ???? into skillsrequired???
}
-
Nov 27th, 2006, 12:02 PM
#13
Stuck in the 80s
Re: Passing more than one textarea variable?
You need to setup your text areas as an array (as I have shown above). Then loop the checked boxes and use the ID (which is the value of the check box) to get the value entered into the text area.
The array for the textarea is an associative array using skillids as the index. So $txt[$skillid] is the value of the textarea for $skillid.
I would have to see your entire block of code to tell you why it is not working.
-
Nov 27th, 2006, 12:16 PM
#14
Thread Starter
Hyperactive Member
Re: Passing more than one textarea variable?
Addposts.php:
Code:
<?php
$query="SELECT * FROM Skills";
$result=mysql_query($query) or die( "Unable to do query");
$num=mysql_num_rows($result);
if ($num==0) {
echo "The database contains no skills";
} else {
$i=0;
while ($i < $num) {
$skillid=mysql_result($result,$i,"Skillid");
$skillname=mysql_result($result,$i,"Skillname");
echo "<input name='skills[]' type='checkbox' onclick='this.form.txt$skillname.disabled = !this.checked' value='$skillid' checked>$skillname: <textarea rows='3' cols='20' name='txt[$skillname]'></textarea><br />";
$i++;
}
mysql_close();
}
?>
and then I call it like this:
Postme.php
(top)
Code:
$skills=$_POST['skills'];
$txt = $_POST[ "txt" ];
And further down;
Code:
$query="SELECT Postid FROM Posts WHERE Userid='$user' && Title='$title'";
$result=mysql_query($query) or die( "No Postid");
$postid=mysql_result($result,$i,"Postid");
if( isset($skills)) // if none are checked, will be false
{
foreach($skills as $skillid) // loop each checked box
{
//$comment = $txt[$skillid];
$query = "INSERT INTO Skillsrequired VALUES ('', '$postid','$skillid','$txt[$skillid]')";
mysql_query($query);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|