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...?
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 )
{
}
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??
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.
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...
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 ];
}
}
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...
Re: Passing more than one textarea variable?
post what you're using on the form to make the textarea's names.
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 />";
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);
}
Re: Passing more than one textarea variable?
Quote:
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>
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???
}
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.
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);
}
}