www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Warning: Illegal string offset

  1. #1
    Join Date
    Sep 2009
    Posts
    25

    Warning: Illegal string offset

    Hello,
    I am a novice with PHP so please bear with me.

    I am trying to build a contact page as part of my PHP learning and have a problem I cannot find an answer for.

    I am using localhost with xampp 1.8.2 and PHP 5.4.22 as my IDE and Firefox with Xdebug extension.

    I am checking my code using phpcodechecker.com but I think perhaps it only checks syntax so although it passes when I analyze, I am getting errors from Xdebug.
    The errors are:
    Warning: Illegal string offset 'Name' in X:\xampp\htdocs\test\contact-us.php on line 16

    this is also on lines 17, 18, 19 and 20.

    The code lines 15 - 21 are

    Code:
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (Trim($Name['Name'])=="")$validationOK=false;
    if (Trim($City['City'])=="")$validationOK=false;
    if (Trim($PostCode['PostCode'])=="") $validationOK=false;
    if (Trim($Tel['Tel'])=="") $validationOK=false;
    if (Trim($Comments['Comments'])=="") $validationOK=false;
    if ($validationOK) $validationOK=$success;
    I think the problem might be within the last line. I set this variable as $success="success";

    I have tried changing as many things as possible to get an answer but it does not work so now I must ask for help.
    I will show the whole script if you would like it.

    thank you
    Ivanna

  2. #2
    Join Date
    Sep 2009
    Posts
    25
    The script works as much as sending the form to me but I do not get the thank you or error message on the browser.

    this is the code that is sending the email followed by the thankyou code.

    Code:
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page or error page
    if ($success){
    header('Location: thank-you.html');
    exit();
    }
    else{
    header('Location: error-message.<html>');
    exit();
    }
    Does anyone know why I get the illegal string error?

    thank you.

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    What is this:

    $Name['Name']

    Do you really have an array called $Name, or $PostCode or $Tel or $City? Because that is what this code looks like it is expecting
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  4. #4
    Join Date
    May 2014
    Posts
    1,038
    That error implies that $name is not an array... Given the rest of what you have there, it seems EXTREMELY unlikely ANY of those are array. MAYBE you just meant to say:

    trim($name)==''

    though really you should use empty...

    empty(trim($name))

    Where are you assigning $name and all the rest of those values?

    Also, since all of them have the same result, why run all of them? That looks like a case for ELSE.

    Without seeing where all those variables are assigned I'm guessing wildly, but I suspect that this:

    Code:
    $validationOK = (
    	empty(Trim($EmailFrom)) ||
    	empty(Trim($Name)) ||
    	empty(Trim($City)) ||
    	empty(Trim($PostCode)) ||
    	empty(Trim($Tel)) ||
    	empty(Trim($Comments))
    ) ? false : $success;
    Is probably what you are trying to do there.
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    Sep 2009
    Posts
    25
    As I have said, I am very novice so I am learning from online tutorials, youtube, books and pieces of code I find anywhere.

    I am trying to take this code and find how it works but maybe I get it wrong because different people do it different ways and some php maybe old?

    I am showing all the code and I have added the code JG gave at the top.

    JG, What does this code do please?

    Deathshadow, I think it should be array, yes?

    Code:
    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');
    
    <?php
    
    // set variables
    
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo = "info@myemail.com";
    $Subject = "Request Form";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $City = Trim(stripslashes($_POST['City'])); 
    $PostCode = Trim(stripslashes($_POST['PostCode'])); 
    $Tel = Trim(stripslashes($_POST['Tel'])); 
    $Comments = Trim(stripslashes($_POST['Comments'])); 
    //$success="success";
    
    // validate field are not empty
    
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (Trim($Name['Name'])=="")$validationOK=false;
    if (Trim($City['City'])=="")$validationOK=false;
    if (Trim($PostCode['PostCode'])=="") $validationOK=false;
    if (Trim($Tel['Tel'])=="") $validationOK=false;
    if (Trim($Comments['Comments'])=="") $validationOK=false;
    //if ($validationOK) $validationOK=$success;
    
    // email body text - Label:, Var, new line
    
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "City: ";
    $Body .= $City;
    $Body .= "\n";
    $Body .= "PostCode: ";
    $Body .= $PostCode;
    $Body .= "\n";
    $Body .= "Tel: ";
    $Body .= $Tel;
    $Body .= "\n";
    $Body .= "Comments: ";
    $Body .= $Comments;
    $Body .= "\n";
    
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page or error page
    
    if ($success){
    header('Location: thank-you.html');
    exit();
    }
    else{
    header('Location: error-message.<html>');
    exit();
    }
    ?>

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    The code I gave you in my signature is PHP code - therefore it has to be inside the php tags at the top. It turns on php's error checking.

    As for your code - what a mess. It's obvious you don't know anything about PHP or probably programming in general. I'll give you these two questions:
    1 - why are you using stripslashes on input? Where did the slashes come from? Do you know that there are slashes? If so, you need to turn off magic quotes!

    2 - why are you trimming 'EmailFrom'? This is a var that you assigned a trimmed value to already, so why do it again?

    There's a lot more wrong with this code, but I'll let you try it first once you get the error checking implemented.

    good luck
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  7. #7
    Join Date
    May 2014
    Posts
    1,038
    These:
    Code:
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo = "info@myemail.com";
    $Subject = "Request Form";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $City = Trim(stripslashes($_POST['City'])); 
    $PostCode = Trim(stripslashes($_POST['PostCode'])); 
    $Tel = Trim(stripslashes($_POST['Tel'])); 
    $Comments = Trim(stripslashes($_POST['Comments']));
    Are NOT arrays. NOTHING you are calling there should be outputting arrays... since it's VERY unlikely that $_POST['Name'] is an array -- $_POST is an array, but an index into it shouldn't be unless you did something like name="City[]" in the markup, which seems unlikely, as that would throw errors when you try to stripslashes and/or trim them as those are string functions NOT array functions.

    Also occurred to me there is no Trim, only trim. Case sensitive language.

    It is a bit of a mess -- PHP code before you even open <?php, variables for nothing, stripslashes for nothing (this isn't 2003), multiple string additions to $body when you don't need to do that, etc, etc...

    REALLY not sure what that $success variable is even for since you re-assign it on attempting to call mail()... without using it. Makes your entire 'check' pointless.

    Guessing slightly, but I suspect what you are trying to do should go a little something like this:

    Code:
    <?php
    
    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');
    
    function headerClean($value) {
    	return preg_replace('/\s+/', ' ', $value);
    }
    
    $fields = ['EmailFrom', 'Name', 'City', 'PostCode', 'Tel', 'Comments'];
    $errors = 0;
    foreach ($fields as $fieldName) {
    	if (isset($_POST[$fieldName])) {
    		if (empty($_POST[$fieldName] = trim(
    			htmlspecialchars($_POST[$fieldName])
    		))) $errors++;
    	} else $errors++;
    }
    
    if ($errors == 0) {
    
    	$mailTo = 'info@myemail.com';
    	$mailSubject = 'Request Form';
    	$mailName = headerClean($_POST['Name']);
    	$mailMail = headerClean($_POST['EmailFrom']);
    	$mailFrom = 'Request Form - ' . $mailName . ' <' . $mailMail . '>';
    
    	$mailHeaders = 
    		'From: ' . $mailFrom . "\r\n" .
    		'Reply-To: ' . $mailMail . "\r\n" .
    		'X-Mailer: PHP/' . phpversion();
    		
    	$mailBody = '';
    	foreach ($fields as $fieldName) {
    		$mailBody .= $fieldName . ': ' . $_POST[$fieldName] . "\r\n";
    	}
    
    	if (mail($mailTo, $mailSubject, $mailBody, $mailHeaders)) {
    		include('thank-you.html');
    		exit();
    	);
    	
    }
    
    include('error-message.html');
    
    ?>
    Skipping those stupid malfing 'header' redirects -- I figure out who's been propagating that halfwit technique of handling PHP results from a function... it's not going to be pretty. Likely not your fault, you're learning from a bad source!

    Though really I'd have a lot more going on there in terms of checks, and callbacks to the originating form. See this mailer I did here for someone:
    http://www.cutcodedown.com/for_other...emUllahHashmi/

    Which is far, far more robust in what it does. (the .source files are, well... the source).
    Java is to JavaScript as Ham is to Hamburger.

  8. #8
    Join Date
    Sep 2009
    Posts
    25
    JG
    PHP code - therefore it has to be inside the php tags I saw it as soon as I posted. my mistake
    As for your code - what a mess. I know.
    It's obvious you don't know anything about PHP but I want to learn very much.

    1. The stripslashes, they were already there in the code I am trying to fix. Perhaps they should not be there. There is no database.

    2. Again, it was already there.

    get the error checking implemented. I have put the code inside the php tag at the top but where do I see the errors? I am outputting to Firefox. I did have Xdebug. That is now disabled but I am still seeing the same error messages in the same format so perhaps Xdebug is doing the same as your code?

    Deathshadow
    unless you did something like name="City[]" No, the markup is name="City"
    not sure what that $success variable is You will see it is cancelled // out. I was trying to find does what.

    Thank you both for helping.

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    The php errors will show up in your browser when you attempt to run the script.

    As for the things you take no responsibility for saying they were already there - get rid of them. LEARN how and write your own code.

    1 - design your html screen and write that html code
    2 - decide what you want to do with the input from that screen and write some php code to receive the input and do something with it.
    3 - design your output screen and write that html code and then output it once step 2 is done, incorporating any new data (from a query perhaps?) into the html via php vars. Keep your php and html separate from each other as much as possible.

    Each of these steps can be a separate file or you can put it all in one script (using functions perhaps) and handle it all in one place.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  10. #10
    Join Date
    Sep 2009
    Posts
    25
    JG,
    Okay,
    I have removed all the trim stuff and got it down to this:

    // set variables
    $Name = ($_POST['Name']);

    // validate
    if ($Name=="")$validationOK=false;

    At least now I am not getting error messages.

    What else is to be cleaned? Most of it I am sure.

    Deathshadow,

    I will also like to try your code on a different form.

    thank you both.

  11. #11
    Join Date
    May 2014
    Posts
    1,038
    Quote Originally Posted by deathshadow View Post
    Also occurred to me there is no Trim, only trim. Case sensitive language.
    Apologies on this one, apparently PHP is inconsistent with C syntax conventions on this, and allows library functions to be in lower case unlike every other language ... despite enforcing case sensitivity on variables :/

    Though for some reason, my PHP 5.5 testing server is throwing an error on them, while my two PHP 5.4 rigs are not... change in the language or banjaxed PHP install?

    Not that you have anything better to do than stand around listening to me quote myself.
    Java is to JavaScript as Ham is to Hamburger.

  12. #12
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    Why did you remove the trim function calls? Any reason, since it is a good thing to do to avoid extraneous chars in your input?

    Can we see your 'new' code once you have cleaned up the php errors?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  13. #13
    Join Date
    Sep 2009
    Posts
    25
    Here is my new code. I have put Trim back in.

    Code:
    <?php
    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');
    
    // set variables
    $EmailFrom = ($_POST['EmailFrom']); 
    $EmailTo = "info@localhost";
    $Subject = "Request Form";
    $Name = ($_POST['Name']); 
    $City = ($_POST['City']); 
    $PostCode = ($_POST['PostCode']); 
    $Tel = ($_POST['Tel']); 
    $Comments = ($_POST['Comments']); 
    
    // validate 
    $validationOK=true;
    if (trim($EmailFrom=="")) $validationOK=false;
    if (trim($Name==""))$validationOK=false;
    if(trim($City==""))$validationOK=false;
    if (trim($PostCode=="")) $validationOK=false;
    if (trim($Tel=="")) $validationOK=false;
    if (trim($Comments=="")) $validationOK=false;
    
    // email body text - Label:, Var, new line
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "City: ";
    $Body .= $City;
    $Body .= "\n";
    $Body .= "PostCode: ";
    $Body .= $PostCode;
    $Body .= "\n";
    $Body .= "Tel: ";
    $Body .= $Tel;
    $Body .= "\n";
    $Body .= "Comments: ";
    $Body .= $Comments;
    $Body .= "\n";
    
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page or error page
    if ($success){
    header('Location: thank-you.html');
    exit();
    }
    else{
    header('Location: error-message.<html>');
    exit();
    }
    ?>
    I am not getting PHP errors.
    I am getting an email.
    I am getting a 'thank you' screen.

    I am NOT getting an error message screen when 'Tel' field is blank. I have not tried the other fields.

  14. #14
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,366
    Thought about trying something like this
    PHP Code:
    <?php
    error_reporting
    (E_ALL E_NOTICE);
    ini_set('display_errors''1');

    function 
    ok(d){ // is it empty?
        
    return empty( trim(d) );
    }

    function 
    sanitize(d){ // token sanitary operation
        
    return trimstripslashes(d) );
    }

    // use a whitelist
    $whitelist = array('EmailFrom'=>'','Name'=>'','City'=>'','PostCode'=>'','Tel'=>'','Comments'=>'');

    // set a validation flag
    $validateOK true;

    // iterate the whitelist and clean input and validate if empty fields present.
    foreach($whitelist as $key=>&$data){
        
    $data =        sanitize$_POST$key ]  );
        
    $validateOK =     ok($data) ? false $validateOK;
    }

    if(
    $validateOK){
        
    $EmailTo =    "info@localhost";
        
    $Subject =    "Request Form";
        
    $Headers =    "From: ".$whitelist['EmailFrom']."\r\n" .
                    
    "Reply-To: ".$whitelist['EmailFrom']."\r\n" .
                    
    "X-Mailer: PHP/".phpversion();
        
        
    $body =        "";    
        
        
    // build the body output
        
    for($whitelist as $keys=>$ok$body .= "{$keys}{$ok}\r\n";
        
        
    // send it
        
    $success mail($EmailTo$Subject$Body$Headers);
        
        
    // redirect to success page or error page
        
    $result = ($success) ? "thank-you.html" "error-message.html";
        
        
    // go to the page depending on the success
        
    header("Location: {$result}");
    }
    /* 
        if we get here, something went wrong in validation, perhaps header"Location: index.php")
        make life hard by sending them to the home page and not your contact page.
    */
    header("Location: index.php");
    ?>
    Last edited by \\.\; 07-24-2014 at 02:32 PM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  15. #15
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    Have you read ANYTHING in the php manual???? If you did you would know how to use the trim function. When you FIRST grab it from the $_POST array, that's when you trim it!! Not later on in every later usage of it!!! Jeez!!!

    What the h.... is 'validationok' meant accomplish for you?? You spend all that time checking things and setting this switch, but then YOU NEVER CHECK IT. What was the point???

    You check at the end whether the email was sent using $success. Why??? I thought you wanted to check if the DATA WAS CORRECT FIRST?

    Do you really want to be a programmer?? Wise up and do some reading on the very basic elements of what it means to write a program. Do yourself a favor. Right now it appears that you would have a hard time following the manual at McDonalds
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles