Web Hosting Talk







View Full Version : explode with arrays


stuffradio
04-03-2007, 06:28 PM
Hi,

I'm trying to get a list of all the values in a textbox that has some text.

Inputbox:
text; text1; text2;

I post this to another page as an array.... called recepient[]. How do I get the values of this textbox?

ISPserver
04-03-2007, 07:24 PM
What language you need? php? perl? javascript?

ISPserver
04-03-2007, 07:25 PM
php and perl has function explode()

stuffradio
04-03-2007, 08:52 PM
I tried using explode with php, but it's not working....

page1:

<input type="text" name="recepient">


page2:

$recepient1 = explode(';', $recepient);
foreach($recepient1 As $rec) {
echo "$rec, ";
}


all it echoes is, ","

what am I doing wrong?

ISPserver
04-03-2007, 09:23 PM
I create file:
<?
$recepient="one;two;tree;";
$recepient1 = explode(';', $recepient);
foreach($recepient1 As $rec) {
echo "$rec, ";
}

?>

And after run I saw:
one, two, tree, ,

Does register_globals rnabled in server?
"echo $recepient" work?

horizon
04-03-2007, 09:26 PM
Try:


if (is_array($recipient)) {
reset($recipient);

foreach ($recipient as $key => $val) {
if (preg_match("/[^;]/i", $val)) {
$val = str_replace(";", "", $val);
echo $val."<br />";
}
}
}

stuffradio
04-03-2007, 09:39 PM
it says one, two, three, so obviously the array does work....
horizon your example doesn't work + you forgot an }

horizon
04-03-2007, 09:43 PM
+ you forgot an }


There are three conditions statement. There are three closing brackets in the end.

stuffradio
04-03-2007, 11:20 PM
I think it was something on something I had in my code, lol.. my bad :p

stuffradio
04-03-2007, 11:41 PM
forgot to edit this.... horizon your example doesn't work, but ISPservers does

Adam Bray
04-04-2007, 04:55 PM
Try this, its similar to ISPservers but in the example you posted it has value1; value2; value3.. so there is a space between ; and the next value.

$recepient1 = explode('; ', $recepient);
foreach($recepient1 As $rec)
{
echo "$rec, ";
}

stuffradio
04-04-2007, 10:20 PM
Nope, I've tried that already... doesn't work :S

foobic
04-04-2007, 10:38 PM
First find out what your form is giving you:
print_r($recepient);
If that gives you nothing try:
print_r($_POST);

stuffradio
04-04-2007, 10:54 PM
well I just figured it out.. turns out naming the textbox recepient[] is a bad thing... lol

Now for the life of me I can't figure out why I can't have files attached to the email :S

Trying to get a possibility of 4 files attached, but don't know how.

horizon
04-05-2007, 05:08 AM
I remember posting a similar request as yours in the past on this forum.

Here's the topic to help you out on this:

http://www.webhostingtalk.com/showpost.php?p=4094807&postcount=78

- posted by inimino which gave great inputs on how to accomplish this task. ;)

stuffradio
04-05-2007, 07:09 PM
How can I make this code send 4 links in one email instead of 4 emails with one link, and how can I make it only display one message if the email didn't send properly :S lol


<?php
require_once('config.php');
$recip = $_POST['recepient'];
$subject = $_POST['subject'];
$reply = "From: $_POST[reply]";

$uploaddir = "/files/";
$uploadfile = $uploaddir . basename($_FILES['file1']['name']);

if ($user[permission] == "0") {
header("Location: mem_desktop.php");
}
if ($_POST['Send']) {
$target = "files/";
$target = $target . basename($_FILES['file1']['name']);
$_FILES['file1']['tmp_name'];

foreach($_FILES["file1"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file1"]["tmp_name"][$key];
$name = $_FILES["file1"]["name"][$key];
move_uploaded_file($tmp_name, "files/$name");
echo "$name was uploaded successfully!<br />";
$message = "$_POST[message]
http://beta.mountroyalstation.ca/members/files/$name";
} else {
echo "There was an error in uploading the file.";
}
$recip1 = explode(';', $recip);
foreach ($recip1 As $rec) {
if (mail($recip, $subject, $message)) {
echo "<br />Message was successfully sent to $rec.";
} else{
echo "Failed to send message.";
}
}
}
}
?>

horizon
04-05-2007, 07:52 PM
Before considering to send emails once for all actions, I'd recommend to increase your conditions since this is about file uploads.



<?php
require_once('config.php');
$recip = $_POST['recepient'];
$subject = $_POST['subject'];
$reply = "From: $_POST[reply]";

$uploaddir = "/files/";
$uploadfile = $uploaddir . basename($_FILES['file1']['name']);

if ($user[permission] == "0") {
header("Location: mem_desktop.php");
}
if ($_POST['Send']) {
$target = "files/";
$target = $target . basename($_FILES['file1']['name']);
$_FILES['file1']['tmp_name'];

foreach($_FILES["file1"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file1"]["tmp_name"][$key];
$name = $_FILES["file1"]["name"][$key];
move_uploaded_file($tmp_name, "files/".$name);
echo "$name was uploaded successfully!<br />";
$message = "$_POST[message]
http://beta.mountroyalstation.ca/members/files/$name";
} else {
echo "There was an error in uploading the file.";
}
$recip1 = explode(';', $recip);
foreach ($recip1 As $rec) {
if (mail($recip, $subject, $message)) {
echo "<br />Message was successfully sent to $rec.";
} else{
echo "Failed to send message.";
}
}
}
}
?>

for:


<?php
@include_once('config.php');
$recip = (isset($_POST['recepient'])) ? (stripslashes(trim($_POST['recepient']))) : "";
$subject = (isset($_POST['subject'])) ? (stripslashes(trim($_POST['subject']))) : "";
$reply = "From: ".(isset($_POST['reply'])) ? (stripslashes(trim($_POST['reply']))) : "";

$error = array();

$uploaddir = "/files/";
if (is_file($uploaddir . $_FILES['file1']['name']) && @is_uploaded_file($uploaddir . $_FILES['file1']['name'])) {
$uploadfile = $uploaddir . basename(stripslashes($_FILES['file1']['name']));
} else {

$error[] = "Error uploading file.";
}

if ($user['permission'] == 0) {
echo "<script>window.location=\"mem_desktop.php\"</script>";
}

if (!preg_match("/^\w(\.?[\w-])+@\w(\.?[\w-])+\.[a-z]{2,4}(\.[a-z]{2})?$/i", $recip)) {
$error[] = "You did not specified a valid email format.";
}

if (!preg_match("/[^a-z0-9]/i", $subject)) {
$error[] = "The subject does not match valid characters. Please try again.";
}

if (!preg_match("/^\w(\.?[\w-])+@\w(\.?[\w-])+\.[a-z]{2,4}(\.[a-z]{2})?$/i", $reply)) {
$error[] = "You did not specified a valid email format.";
}

if (!preg_match("/[a-zA-Z0-9\.\,;:%&#@!\^-_~`\"'\[\]\{\}\*\/\?\(\)\n\r]/", $_POST['message'])) {
$error[] = "The message text does not contain valid characters.";
}

if (isset($_POST['send']) && !empty($_POST['Send'])) {
$target = "files/";
if (is_file($target . $_FILES['file1']['name']) && is_uploaded_file($target . $_FILES['file1']['name'])) {
$target = $target . basename(stripslashes($_FILES['file1']['name']));
} else {

$error[] = "Error uploading file.";
}

foreach($_FILES["file1"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = basename(stripslashes($_FILES["file1"]["tmp_name"][$key]));
$name = basename(stripslashes($_FILES["file1"]["name"][$key]));
@move_uploaded_file($tmp_name, "files/$name");
$error[] = $name." was uploaded successfully.";
$message = nl2br($_POST[message])."
http://beta.mountroyalstation.ca/members/files/".$name;
} else {
$error[] = "Error uploading file.";
}
$recip1 = explode(';', $recip);
foreach ($recip1 As $rec) {
if (mail($recip, $subject, $message)) {
$error[] = "Message was successfully sent to ".$rec.".";
} else{
$error[] = "Failed to send message.";
}
}
}
}

echo sprintf("%s", $error)."<br />";
?>

;)

Then, to answer your question, the problem is located here:


foreach ($recip1 As $rec) {
if (mail($recip, $subject, $message)) {
$error[] = "Message was successfully sent to ".$rec.".";
} else{
$error[] = "Failed to send message.";
}
}


it will check for each variables for each captured recipient email address (which uses a lot of email ressources though).

stuffradio
04-05-2007, 08:06 PM
I get a blank page with the text Array :S

I guess the $error array isn't set properly or echoed properly

horizon
04-05-2007, 08:10 PM
My bad.

Replace this line:


echo sprintf("%s", $error)."<br />";


with:


echo printf("%s", $error)."<br />";


This should correct the problem.

stuffradio
04-05-2007, 08:22 PM
now it says Array5 lol

horizon
04-05-2007, 09:03 PM
Replace:


echo printf("%s", $error)."<br />";

with:


if (is_array($error)) {
foreach ($error as $key => $val) {
printf("<li>%s</li>\n", $val);
}
}

Should output the right lines now. ;)

stuffradio
04-05-2007, 11:33 PM
ok it outputs the lines... but now nothing works :S
Error uploading file.
You did not specified a valid email format.
The subject does not match valid characters. Please try again.
You did not specified a valid email format.

horizon
04-06-2007, 09:27 AM
What kind of characters do you use else than a-zA-Z0-9 for your subject line and what kind of output do you use for your emails ?

stuffradio
04-06-2007, 01:24 PM
for subject I'm just using the normal characters, "This is a subject"

for message what do you mean what kind of output do I use?

horizon
04-06-2007, 01:28 PM
for message what do you mean what kind of output do I use?


I did not mentionned anything about the message but the recipient email and reply email. What email format did you used so that the validations would be denyed ?

stuffradio
04-06-2007, 01:40 PM
ok nevermind... I was just trying to have the reply to part a string... no email address. I tried using a valid email address this time, and that part worked,

Still have this problem:

Error uploading file.
The subject does not match valid characters. Please try again.

Does it allow for spaces in the subject?

horizon
04-06-2007, 02:14 PM
Does it allow for spaces in the subject?


Replace:


if (!preg_match("/[^a-z0-9]/i", $subject)) {


with:


if (!preg_match("/[^a-z0-9\ ]/i", $subject)) {


This should correct the problem.

stuffradio
04-09-2007, 03:50 PM
no, that still doesn't work... trying to read up on regex to see if I can figure it out, but most likely I won't be able to :lol:

horizon
04-09-2007, 06:43 PM
If you replace:


if (!preg_match("/[^a-z0-9\ ]/i", $subject)) {


with:


if (!preg_match("/[^a-z0-9/\s/]/i", $subject)) {


What does it say ?

stuffradio
04-09-2007, 07:47 PM
The subject does not match valid characters. Please try again.

horizon
04-09-2007, 08:02 PM
As stupid as it sounds,

replace:


if (!preg_match("/[^a-z0-9/\s/]/i", $subject)) {


with:


if (preg_match("/[^a-z0-9/\s/]/i", $subject)) {


Forgot to remove the ! since the ^ command is already contradictory. ;)

stuffradio
04-09-2007, 08:05 PM
rofl wow that does work!

However file uploading doesn't work still :S

horizon
04-09-2007, 09:35 PM
Replace all instences of:


if (!preg_match(


to read:


if (preg_match(


(Simply remove the ! from each of my additional replacements).

stuffradio
04-10-2007, 05:57 PM
I tried that but it doesn't make any difference

horizon
04-10-2007, 07:26 PM
Let's try a new angle.

Replace:


if (preg_match("/[^a-z0-9/\s/]/i", $subject)) {


with:


if (eregi("(^a-z0-9/\s)$", $subject)) {


Will it still output the same error message ?

stuffradio
04-10-2007, 07:28 PM
no no lol,

this is for the file uploading not the subject.
Subject works fine

horizon
04-10-2007, 07:32 PM
Ahh ! yes. This is an exception case though:


if (!preg_match("/[a-zA-Z0-9\.\,;:%&#@!\^-_~`\"'\[\]\{\}\*\/\?\(\)\n\r]/", $_POST['message'])) {
$error[] = "The message text does not contain valid characters.";
}


The ! must remain there on this one. Sorry about that. :)

stuffradio
04-10-2007, 07:37 PM
alright no problem :)

That works again :P

Now just trying to get the file uploading part working ;)

horizon
04-10-2007, 08:04 PM
Now just trying to get the file uploading part working


What is the problem regarding file uploads actually (technicly asking) ?

stuffradio
04-10-2007, 08:10 PM
it displays can't upload file all the time.

horizon
04-10-2007, 08:15 PM
I do not see a line mentionning upload limitations in the script I coded here. Is it a server error message or coded from one of your PHP files outside this one ?

stuffradio
04-10-2007, 08:18 PM
$uploaddir = "/files/";
if (is_file($uploaddir . $_FILES['file1']['name']) && @is_uploaded_file($uploaddir . $_FILES['file1']['name'])) {
$uploadfile = $uploaddir . basename(stripslashes($_FILES['file1']['name']));
} else {
$error[] = "Error uploading file.";
}

horizon
04-10-2007, 08:32 PM
I believe the files are not being uploaded at all. Did you set the proper CHMOD settings to your upload folder in order to be able to add some files in it ?

Hint: Set CHMOD on your upload folder to: 755. This should resolve the problem.

In the mean time,

replace :


$uploaddir = "/files/";
if (is_file($uploaddir . $_FILES['file1']['name']) && @is_uploaded_file($uploaddir . $_FILES['file1']['name'])) {
$uploadfile = $uploaddir . basename(stripslashes($_FILES['file1']['name']));
} else {
$error[] = "Error uploading file.";
}
with:


$uploaddir = "/files/";
if (is_file($uploaddir . $_FILES['file1']['name']) && @is_uploaded_file($uploaddir . $_FILES['file1']['name'])) {
$uploadfile = $uploaddir . basename(stripslashes($_FILES['file1']['name']));
$error[] = "File uploaded successfully.";
} else {
$error[] = "Error uploading file.";
}
This should help you out to see if the files has been successfully uploaded or not once the CHMOD settings has been assigned on the upload folder. ;)

stuffradio
04-10-2007, 09:01 PM
I will try it soon and let you know.

Before I did any adjustments to the script the file uploading was working fine,

after I did the adjustments ie replacing the file with what you told me to replace it with, you had something that made it not work.

Also it already is set to 0755

horizon
04-10-2007, 09:08 PM
Very well. Keep this thread updated. .;)

stuffradio
04-11-2007, 01:31 AM
It still doesn't upload properly :S

Error uploading file.

horizon
04-11-2007, 08:00 AM
If you go to your FTP / cPanel, do you see your uploaded file under your upload folder ?

stuffradio
04-11-2007, 10:34 PM
Nope, I don't see any of the recent files I tried uploading in there

horizon
04-12-2007, 03:42 PM
See this URL for more info on file uploads:

http://www.w3schools.com/php/php_file_upload.asp

From there, you could replace your current file uploads codings with the one mentionned from the URL. ;)