The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

2 Pages V  1 2 >  
Reply to this topicStart new topic
> upload multiple textboxes?
xxkasperxx
post Jun 27 2012, 11:30 AM
Post #1


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



How can i upload more than 1 text box into my database? i want to have it to where they are seperrate fields..

EX: |--------------------------------------------|
| FIELD 1.. so text box 1 goes here |
|--------------------------------------------|
|--------------------------------------------|
| FIELD 2.. so text box 2 goes here |
|--------------------------------------------|

But the user is going to be able to add more text boxes. It is for an announcements page.

So the big question is how do i upload the text-boxes into the correct field?

CODE
<?php
session_start();
include("config.php");
$query = mysql_query("SELECT * FROM home");
$query_row=mysql_fetch_array($query) or die (mysql_error());
if (isset($_SESSION['loggedin'])&&isset($_SESSION['username']))
{
echo "Welcome, " . $_SESSION['username'] . "!" . "<a href='logout.php'> Log-out!</a>";

?>
<!-- EDIT FOR CMS -->

<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>

</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <form action="" method="POST">
        <?php
        $counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
            echo '<textarea id="text.$counter" name="text" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            
            
    
            }
        ?>
        <input type="submit" value="Submit" name="submit">
        </form>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}else
{
?>

<!-- REGULAR -->
<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <?php
        while($row = mysql_fetch_array($query)){
            echo $row['content'];
            }
        ?>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}

    if ($_POST)
    {
    /*
        foreach ($text as $text){
        $text = $_POST['text'];
        echo $text;
        echo "data is posted";
        if (isset($text))
        {
            echo 'text is set';
            $result = strip_tags(mysql_query("UPDATE home SET content = '$text'"));
            
            
        }
    }*/
    

        

    }
    
    
    
    


    

?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 28 2012, 05:36 PM
Post #2


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



i have tryed to de-bug this... and it seems like it should work because it is getting the text from the textarea with the name of $counter...
CODE
<?php
session_start();
include("config.php");
$query = mysql_query("SELECT * FROM home");
$query_row=mysql_fetch_array($query) or die (mysql_error());
if (isset($_SESSION['loggedin'])&&isset($_SESSION['username']))
{
echo "Welcome, " . $_SESSION['username'] . "!" . "<a href='logout.php'> Log-out!</a>";

?>
<!-- EDIT FOR CMS -->

<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>

</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
            </ul>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <form action="" method="POST">
        <?php
        $counter =1;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                    echo "<br>".$counter."<br>";
            echo '<textarea id="text.$counter" name="$counter" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "name: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    if ($counter==$tbNum)
                    {
                        echo "<br>Counter: ".$counter."<br>";
                        $text = $_POST['$counter'];
                        echo $text ." <br>";
                        echo $counter.$tbNum;
                    }
                
                    //$result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE id = '$tbNum'"));
        
    
        }
            }
        ?>
            <input type="submit" value="Submit" name="submit">
        </form>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}else
{
?>

<!-- REGULAR -->
<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
            </ul>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <?php
        while($row = mysql_fetch_array($query)){
            echo $row['content'];
            }
        ?>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jun 29 2012, 12:07 AM
Post #3


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
i have tryed to de-bug this


That's a good start. (Though you should know 'I' is a capital, and tryed -> tried.)

But please don't just post the whole program and expect people to wade through working out what's what.

First thing I notice is that you are 'echo'ing the logon info before starting the html document ("<html>"). You shouldn't echo any text as part of the output page until you get to "<body>".

CODE

<form action="" method="POST">


Second: why does the form have no action?

Third: you are looping through outputting textareas, but giving them all the same name. This obviously won't work.

HTH
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 29 2012, 11:54 AM
Post #4


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



The text areas are named "text" and have a number after them. and it starts from 2 and goes until there are no more. so the text boxes would be like "text2" "text3" but i don't get how i can take all of their text from each separate box, because the user will be adding more boxes. So i don't get how i can pull the text from each box, upload it then move onto another box.

Please help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 29 2012, 01:50 PM
Post #5


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



QUOTE

Second: why does the form have no action?



Because it is being processed on the same page.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 29 2012, 02:02 PM
Post #6


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



QUOTE(Brian Chandler @ Jun 29 2012, 07:07 AM) *

why does the form have no action?

If it submits to the same URL it shouldn't be necessary in HTML4, according to http://www.thefutureoftheweb.com/blog/use-...bmit-to-current

But it appears HTML5 requires a non-empty value, even though the ACTION attribute itself appears to be optional:
"The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces."
http://www.w3.org/TR/html5/attributes-comm...r-fs-formaction
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 29 2012, 02:29 PM
Post #7


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



QUOTE(xxkasperxx @ Jun 29 2012, 06:54 PM) *

The text areas are named "text" and have a number after them. and it starts from 2 and goes until there are no more. so the text boxes would be like "text2" "text3"

In your second example the NAME uses nothing but the $counter variable, which is just an integer:

CODE
echo '<textarea id="text.$counter" name="$counter"

Also note that variables are not expanded in single quoted strings: http://www.php.net/manual/en/language.types.string.php

Instead I believe you want something like this:

CODE
echo '<textarea id="text'.$counter.'" name="text'.$counter.'"


QUOTE
but i don't get how i can take all of their text from each separate box, because the user will be adding more boxes. So i don't get how i can pull the text from each box, upload it then move onto another box.

I'd use the square bracket method suggested here: http://www.php.net/manual/en/faq.html.php#faq.html.arrays

If you instead add a number after each NAME value, you'll need to let the form handling script increment the number values until the last one is found. Maybe you can use isset() to test that. unsure.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 29 2012, 02:51 PM
Post #8


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



It keeps give me a error message of

"Notice: Undefined index: text'.1.' in C:\wamp\www\ConroeGoAway\index.php on line 64"

and this is line 64

"$text = $_POST["text'.$counter.'"];"

I have no clue what to do.. To me it all makes sense because the names should match up.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 29 2012, 03:57 PM
Post #9


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



CODE
"$text = $_POST["text'.$counter.'"];"

The above should be

CODE
$text = $_POST["text$counter"];

or
CODE
$text = $_POST['text'.$counter];

See the page I linked to on how variables interact with single- or double quote string syntax.

Don't forget to sanitize the SQL query with mysql_real_escape_string().
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 29 2012, 04:09 PM
Post #10


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



So now I get to press submit with no errors, but it still wont change the data for each box individually.. it still does them as a whole(every box with the same content)

CODE

$counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                //    echo "<br>".$counter."<br>";
                echo $counter;
            echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            echo $counter;
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                //    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                
                        $text = $_POST["text$counter"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE $tbNum == $counter  "));
                    }
                
                    
        
    
        }
            }
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jun 30 2012, 01:44 AM
Post #11


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



Sorry, I forgot about the empty action thing.

QUOTE(Christian J @ Jun 30 2012, 04:29 AM) *


Instead I believe you want something like this:

CODE
echo '<textarea id="text'.$counter.'" name="text'.$counter.'"



Yes. Or:
CODE
echo "<textarea id=\"text$counter\" name=\"text$counter\"...


Which way to go is the Really Big Decision of PHP programming happy.gif

QUOTE

QUOTE
but i don't get how i can take all of their text from each separate box, because the user will be adding more boxes. So i don't get how i can pull the text from each box, upload it then move onto another box.

I'd use the square bracket method suggested here: http://www.php.net/manual/en/faq.html.php#faq.html.arrays
If you instead add a number after each NAME value, you'll need to let the form handling script increment the number values until the last one is found. Maybe you can use isset() to test that. unsure.gif


The "square bracket trick" is a *very* ugly hack. Much neater and simpler to loop through all the $_POST variables. (imo)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Jun 30 2012, 08:30 AM
Post #12


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



How does the user add more text boxes?

The only way I can think of is to interact with the server for each new text box. That means that the server can count the text boxes before it sends another page.

The problem then goes away, doesn't it?

Another way is to allow the user to enter more than one 'text box's worth of data in one text box. Then the problem is to parse one text box worth of multiple textual info. This is a bad way. Every user will use a different mental pattern to make multi 'text box's pattern. This can turn into a parsing nightmare.

FWIW

This post has been edited by Ephraim F. Moya: Jun 30 2012, 08:35 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 30 2012, 12:30 PM
Post #13


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



There is only one user, and there would be a textbox that is empty and they type their text in there and press upload. It will then send the info to the database with the insert function. And then work just like the other textboxes.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 30 2012, 01:43 PM
Post #14


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



QUOTE(Brian Chandler @ Jun 30 2012, 08:44 AM) *

Which way to go is the Really Big Decision of PHP programming happy.gif

I prefer single quote syntax when there are more double quotes than variables in the string, otherwise double quote syntax. Occasionally I split a string so I can use different syntax for each part.

There's also Heredoc and Nowdoc syntaxes, but I haven't tried them yet.

QUOTE
The "square bracket trick" is a *very* ugly hack.

What's ugly and hacky with it? sad.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 30 2012, 01:44 PM
Post #15


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



QUOTE(Ephraim F. Moya @ Jun 30 2012, 03:30 PM) *

How does the user add more text boxes?

The only way I can think of is to interact with the server for each new text box.

You can generate them with javascript as well.

In any case you may want to impose an upper limit, to prevent flooding.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 30 2012, 02:47 PM
Post #16


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



so i still havent figured out how to update what is in each textbox yet. i can update them. BUT it only takes the text from the last box.. how do i get it to take the text from each box and update it to the correct box?
This is what i have so far..

CODE

        $counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                //    echo "<br>".$counter."<br>";
                //echo $counter;
            echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            //echo $counter;
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                
                        $text = $_POST["text$counter"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE '$tbNum' = '$counter'"));
                    }
                
                    
        
    
        }
            }
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Jun 30 2012, 03:29 PM
Post #17


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(Christian J @ Jun 30 2012, 12:44 PM) *

QUOTE(Ephraim F. Moya @ Jun 30 2012, 03:30 PM) *

How does the user add more text boxes?

The only way I can think of is to interact with the server for each new text box.

You can generate them with javascript as well.

In any case you may want to impose an upper limit, to prevent flooding.


Either php or Javascript means that there's a computer involved. Then every textarea can have a unique name. That's all that's required.

Javascript SUCKS -- probably the WORST computer language ever devised.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jun 30 2012, 05:48 PM
Post #18


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



QUOTE(xxkasperxx @ Jun 30 2012, 02:47 PM) *

so i still havent figured out how to update what is in each textbox yet. i can update them. BUT it only takes the text from the last box.. how do i get it to take the text from each box and update it to the correct box?
This is what i have so far..

CODE

        $counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                //    echo "<br>".$counter."<br>";
                //echo $counter;
            echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            //echo $counter;
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                
                        $text = $_POST["text$counter"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE '$tbNum' = '$counter'"));
                    }
                
                    
        
    
        }
            }


still havent figured it out
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 1 2012, 03:14 PM
Post #19


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



QUOTE(xxkasperxx @ Jul 1 2012, 12:48 AM) *

still havent figured it out

My SQL is a bit rusty so I can't help, sorry. However it sounds like the content of the TEXTAREAs are overwriting each other. Test if it happens already in the PHP or in the SQL query.

You may want to provide neater code examples, as a courtesy to others viewing it. For example, why include both these lines:

CODE
//    echo "<br>".$counter."<br>";
//echo $counter;

when one would be enough for debugging? Also the current line indentation of the code example is pretty confusing to follow.

And again, you need to sanitize the content sent to the DB to avoid SQL injection exploits. strip_tags() will not help with that.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
xxkasperxx
post Jul 2 2012, 09:03 AM
Post #20


Serious Coder
*****

Group: Members
Posts: 261
Joined: 30-April 11
Member No.: 14,449



so i tryed to see where stuff was going wrong but i keep getting this error:

Undefined index: text'.1.' in C:\wamp\www\ConroeGoAway\index.php on line 69
Undefined index: text'.2.' in C:\wamp\www\ConroeGoAway\index.php on line 69

but when i echo stuff out with this code:

CODE
echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            //echo "counter: ".$counter;
            echo "text'.$counter.'<br>";
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..

                    //echo $text.$counter;
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                        echo "text'.$counter.'";
                        $text = $_POST["text'.$counter.'"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE '$tbNum' = '$counter'"));
                    }
                
                    
        
    
        }


They match up. So why is my $text = $_POST["text'.$counter.'"]; not getting the text?
Please help
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 10:14 PM