Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Textarea form question
Old 04-20-2006, 11:08 PM Textarea form question
Novice Talker

Posts: 10
Trades: 0
I was hoping I could find someon who could tell me if it is possible to have data from a textarea form be split up so it can be inserted into several fields in a mysql db?

Example of some text would be: 123:456:789 text1 text2 text3 text4

Can php split that up into 5 different fields to be inserted into one row of 5 fields?
avis is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-21-2006, 01:47 AM Re: Textarea form question
vangogh's Avatar
Post Impressionist

Latest Blog Post:
Why Responsive Design?
Posts: 10,815
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Will you be able to control the input so you know there will always be a colon or a space between the text you want to split?

If so php has a function called split() that might be exactly what you're looking for. It will split a string of text into an array of values.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 04-21-2006, 03:31 PM Re: Textarea form question
Novice Talker

Posts: 10
Trades: 0
Hi, Thanks for the reply. The data that would be inputted would always be in the same format. In my example 123:456:789 would be one field. The next field would be name etc. All the input is seperated by spaces. I have seen the php split but I am not sure how to use it. Could you please give me an example of how that might work please.
avis is offline
Reply With Quote
View Public Profile
 
Old 04-21-2006, 06:21 PM Re: Textarea form question
vangogh's Avatar
Post Impressionist

Latest Blog Post:
Why Responsive Design?
Posts: 10,815
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
split() will take a text string and split it based on a character you supply and put the results in an array.

so if you have a variable $text that you get from the texarea on your form and it's value is always of the form xxx:xxx:xxx

then you could say:

$data_array = split(":", $text);

You'll end up with an array of values in $data_array. If the value of the string that was stored in $text was 123:456:789 then

$data_array[0] = 123
$data_array[1] = 456
$data_array[2] = 789

The : will be removed completey. Then you would insert the individual array values into your database however you want.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 04-21-2006, 07:08 PM Re: Textarea form question
Novice Talker

Posts: 10
Trades: 0
OhThanks, I have a form now that works pretty well. However sometimes not all the fields have information to be inputted. For example 123:456:789 text1 text2 text3 text4 is split just the way I want but if there is no data for the last text2 text3 or text4 I get this:
Notice: Undefined offset: 3 in c:\inetpub\wwwroot\swarm\insert2.php on line 25
That error only occurs when I don't have data for the last few text data that was split.

Is there a way to avoid this error when only a part of the data is inputted?
ANother thing I get is that there are 4 blank rows added before the data is added.

Again thanks for all your help.

Here is the code:

PHP Code:
<form  method="POST">
<textarea name="textareaname" rows="2" cols="20" accesskey="" tabindex="" 

></textarea>
<br><input type="Submit">
</form>
<?
$username
="root";
$database="coords";
mysql_connect("localhost",$username);
mysql_select_db($database) or die( "Unable to select database");
$mydata = (isset($_POST['textareaname']))?$_POST['textareaname']:'';


$targettemp split('/\n/'$mydata);
foreach(
$targettemp as $d ) {
    
$d1 split(" "$d);
    
$coords $d1[0];
    
$name $d1[1];
    
$owner $d1[2];
    
$defense $d1[3];
    
$comments $d1[4];
    
$query "INSERT INTO planets VALUES 

('','
$coords','$name','$owner','$defense','$comments')";
    
mysql_query($query);
}
    
mysql_close();

?>
THis is on my machine at home so no security risks using root and no password. I have a host that I make sure to use a password and the directory that has access is password protected.
avis is offline
Reply With Quote
View Public Profile
 
Old 04-21-2006, 10:53 PM Re: Textarea form question
Novice Talker

Posts: 10
Trades: 0
I am happy to say that this matter is resolved THe fix was really simple actually. Here is what I had to do:
I changed this:
PHP Code:
$mydata $_POST['textareaname']; 
to:
PHP Code:
$textareaname 'textareaname';
$mydata = (isset($_POST['textareaname']))?$_POST['textareaname']:''
Now the form works great. Thanks for the help.
avis is offline
Reply With Quote
View Public Profile
 
Old 04-22-2006, 01:25 AM Re: Textarea form question
vangogh's Avatar
Post Impressionist

Latest Blog Post:
Why Responsive Design?
Posts: 10,815
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Glad you got it all worked out and glad I was able to help in some way.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Reply     « Reply to Textarea form question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25666 seconds with 12 queries