array is showing empty not giving any value - php

I'm running function to check input empty but problem is that variable in array not working.
Here is my code:
$name = $email = $message = $result = "";
function has_presense($input){
if(empty($input)){
return $result = ucwords($input) ." is missing!";
}
}
if(isset($_POST['send'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$input = array($name,$email,$message);
foreach($input as $key => $value){
if(has_presense($key)){
$result = $value . "is missing";
}
}
}
Result:
Array ( [0] => [1] => [2] => )
it's showing empty array what is problem in it.

Description Make use of var_dump($_POST) or print_r($_POST) which will tell you what is in it if the form submission is successfully done.

The problem is that name, email, and message are empty.
Try:
var_dump($_POST);
to check if your the PHP is recieving everything.

Related

Waring: illegal string offset in 'name' in contactform since PHP7.1

Since the update to php7.1 I get an error on a contactform. Downgrading is not an option. Any solution? I tried some thing I found, but doesn't seem to work what I change.
The message I get is "Warning: illegal string offset in 'name'" and it does this for all the values in the form (name, email, message,...).
<?php
require_once("classes/phpmailer/class.phpmailer.php");
$smarty_mail = new Smarty;
$smarty_mail->template_dir = 'templates/mail';
$smarty_mail->compile_dir = 'pages/templates_c';
if ( isset($_POST['submit']) )
{
$error = '';
print_r($error);
if (!trim($_POST['name'])) $error['name']=true;
if (!check_email($_POST['email'])) $error['email']=true;
if (!trim($_POST['message'])) $error['message']=true;
if (!isset($_POST['privacypolicy'])) $error['privacypolicy']=true;
$_POST['name'] = stripslashes($_POST['name']);
$_POST['message'] = stripslashes($_POST['message']);
if (!$error)
{
$contact = $_POST;
$contact['ip'] = $_SERVER['REMOTE_ADDR'];
$contact['host'] = gethostbyaddr( $contact['ip']);
$smarty_mail->assign("contact", $contact);
$message = $smarty_mail->fetch("mail_contact.tpl.html");
$subject = "contactformulier";
if( sendemail(MAIL_FROM_NAME, MAIL_FROM, $_POST['name'], $_POST['email'], $subject, $message, "HTML", "", ""))
{
$smarty->assign("send", true);
}
}
$smarty->assign("error",$error);
$smarty->assign("set", $_POST);
}
$main_content_template = "contact.tpl.html";
?>
$error = '';
makes no sense. You're initializing $error as a string, but then you're accessing it as if it were an array:
$error['name']=true;
It should probably be
$error = array();
instead.

Correct syntax in Controller.php

In View.php file <?php echo $ticket->player_id ?> is working. But in controller.php it is not working. What is the correct syntax for Controller.php?
I am trying to use it here
'include_player_ids' => array('<?php echo $ticket->player_id ?>'),
The following is the sample code from controller file which are working properly
// Send Email
$email_template = $this->home_model->get_email_template_hook("ticket_reply", $lang);
if($email_template->num_rows() == 0) {
$this->template->error(lang("error_48"));
}
$email_template = $email_template->row();
if(isset($ticket->client_username)) {
$username = $ticket->client_username;
$email = $ticket->client_email;
$first_name = $ticket->first_name;
$last_name = $ticket->last_name;
} else {
$username = $ticket->guest_email;
$email = $ticket->guest_email;
$first_name = $ticket->guest_email;
$last_name = "";
}
In controller.php ,
$data['ticket'] = 'your data will be here';
In view.php,
'include_player_ids' => array($ticket->player_id),
Hope you understand.
Its already in php tags so you do not need to place <?php ?> again just assign variable directly
'include_player_ids' => array($ticket->player_id),
It should be
'include_player_ids' => array($ticket->player_id),
Please post the output of $ticket

array is added to email address

i was using this codes to send newsletter to members , now the problem is that when i send emails it doesn't send . i tracked the emails in cpanel and found that the email address has the word 'array' before it . that means that when i send to 'email#domain.com' it changes to 'arrayemail#domain.com'. and this is the code :
<?php
include("../include/config.php");
include_once("../include/functions/import.php");
verify_login_admin();
$adminurl = $config['adminurl'];
$thebaseurl = $config['baseurl'];
$sql = "SELECT USERID, username, email FROM members";
$executequery = $conn->Execute($sql);
$results= $executequery->getrows();
if($_POST['submitform'] == "1")
{
if(isset($_POST['USERID']))
{
foreach($_POST['USERID'] as $key)
{
$subject = $_POST['subject'];
$sendername = $config['site_name'];
//$bodymessage = "Dear " . $_POST['username'];
//$sendmailbody = "HI". $results[$i]. $key . $results[$i].$_POST['username'] .",";
$sendmailbody .= $_POST['message'];
$sendmailbody .= "";
$from = $config['site_name'].'<'.$config['site_email'].'>';
$sendto = $results[$i]. $key.',';
mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc);
}
}
$message = "E-Mails / Newsletters sent successfully.";
Stemplate::assign('message',$message);
}
$mainmenu = "7";
$submenu = "2";
$bodymsg = $_POST['message'];
Stemplate::assign('subject',$subject);
Stemplate::assign('bodymsg',$bodymsg);
Stemplate::assign('mainmenu',$mainmenu);
Stemplate::assign('submenu',$submenu);
Stemplate::assign('results',$results);
STemplate::display("administrator/global_header.tpl");
STemplate::display("administrator/mass_newsletter.tpl");
STemplate::display("administrator/global_footer.tpl");
?>
It looks like you are getting all rows from your query:
$results= $executequery->getrows();
^ plural
But you never actually loop over all rows, so $results[$i] will contain one complete row from the result set: An array containing the USERID, username and email.
And when you concatenate an array with a string, the array will result in the text array.
And that is assuming that $i is an integer but in your code it is not defined at all.
You probably want to loop over the result-set somewhere and if you use a foreach you will not need the $i variable at all:
foreach ($results as $result) {
// Build your e-mail addresses string?
...
}

Checkbox value not displaying

The form inputs aren't displaying on the form.php page and negates my form validation. The error says undefined variable for all my variables on form.php. Please tell me what I have to edit in my code to make it display the form inputs on form.php. It works when I use it on the same page but I would rather it display on another page.
EDIT
Thanks so far but I can't get the value of the checkbox, the recipient(Administrator or Content Editor), to display it displays "Array" or "A".
contact.php
<?php
$errnam = "";
$errmail = "";
$errsub = "";
$errrec = "";
$hasErrors = false;
if(isset ($_POST['submitted'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$recipient = $_POST['recipient'];
$message = $_POST['message'];
if(preg_match("/^[\w\-'\s]/", $_POST['name'])){
$name = $_POST['name'];
}
else{
$errnam ='<strong>Please enter a name.</strong>';
$hasErrors = true;
}
if (preg_match("/^[\w.-_]+#[\w.-]+[A-Za-z]{2,6}$/i", $email)){
$email = $_POST['email'];
}
else{
$errmail = '<strong>Please enter a valid email.</strong>';
$hasErrors = true;
}
if(preg_match("/^[\w\-'\s]/", $_POST['subject'])){
$subject = $_POST['subject'];
}
else{
$errsub = "<strong>Please enter a subject.</strong>";
$hasErrors = true;
}
if (!empty($_POST['recipient'])) {
for ($i=0; $i < count($_POST['recipient']);$i++) {
$recipient = $_POST['recipient'];
}
}else{
$errrec = "<strong>Please select a recipient</strong>";
$hasErrors = true;
}
$message = $_POST['message'];
}
if ($hasErrors){
echo "<strong>Error! Please fix the errors as stated.</strong>";
}else{
header("Location: form.php?name=".$name."&email=".$email."&subject=".$subject. "&recipient=".$recipient. "&message=".$message);
exit();
}
?>
form.php
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$subject = $_GET['subject'];
$recipient = $_GET['recipient'];
$message = $_GET['message'];
echo "<h2>Thank You</h2>";
echo "<p>Thank you for your submission. Here is a copy of the details that you have sent.</p>";
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong>" .$recipient. "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
?>
If you would like to transfer the data from contact.php to form.php you should use something like this:
contact.php
$data = urlencode(
serialize(
array(
"name" => $name,
"email" => $email,
"subject" => $subject,
"message" => $message)
));
header('Location: form.php?data=' . $data);
form.php
$data = unserialize(urldecode($_GET['data']));
$name = $data["name"];
$email = $data["email"];
$subject = $data["subject"];
$message = $data["message"];
This serializes the array of data from contact.php then URL encodes it and sends it as a GET variable to form.php. After, form.php URL decodes and unserializes the data for use.
The problem is when you header("Location:") to form.php, all the POST values are lost. You have to either resend them with the header, or modify them into GET and retrieve them again. It should be more efficient to have them both (contact.php AND form.php) in one page. That way, the form data only has to be sent once.
You could probably just send the POST values as GET over to form.php like this.
contact.php:
header("Location: form.php?name=".$name."&email=".$email."&subject=".$subject."&message=".$message);
form.php (to retrieve the values):
$name = $_GET['name'];
$email = $_GET['email'];
$message = $_GET['message'];
$subject = $_GET['subject'];
If you want to display form elements then you have to use this approach.
<form method="POST" action="contact.php">
Email<input type="text" name="email">
.......
.......
.......
// All elements
</form>
This may help you.
Give action in your form in contact.php
<form action="form.php">

PHP Feedback form Checkbox error

Ok here is a shortened version of the php for my contact form, (the checkboxes are not being sent through correctly)
<?php
//please fill this in at least!
$myemail = "";
$title = "Feedback Form";
if(isset($_POST['submit'])) { //form has been submitted
//set variables with filters
$cont_name = filter_var($_POST['cont_name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['cont_email'], FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['cont_phone'], FILTER_SANITIZE_STRING);
$first_time = filter_var($_POST['first_time'], FILTER_SANITIZE_STRING);
$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING);
function valid_email($str){
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;}
$errors = 0; //by default there are no errors
$trimcont_name = trim($cont_name);
if(empty($trimcont_name)){
//the name field is empty
$errors = 1; //tips off the error messages below
$errorcont_name = "The name field is empty"; //this error is displayed next to the label
}
if(!valid_email($email)) {
//email is invalid or empty
$errors = 1;
$erroremail = "The email address was not valid";
}
$trimphone = trim($phone);
if(empty($trimphone)){
//the phone field is empty
$errors = 1;
$errorphone = "The phone field is empty";
}
$trimfirst_time = trim($first_time);
if(empty($trimfirst_time)){
//the first_time field is empty
$errors = 1;
$errorfirst_time = "This field is empty";
}
$trimhear_about = trim($hear_about);
if(empty($trimhear_about)){
//the hear_about field is empty
$errors = 1;
$errorhear_about = "This field is empty";
}
if($spam != "") {
//spam was filled in
$errors = 1;
$errorspam = "The Spam box was filled in";
}
if($errors == 0) {
$sendto = $myemail;
$message = <<<DATA
DETAILS
Name: $cont_name
Email: $email
Phone: $phone
Was this the first time you have been to us?
$first_time
How did you hear about us?
$hear_about
DATA;
$headers = 'From: ' . $name . '<' . $email . '>';
if(mail($sendto, $title, $message, $headers)) {
//this is where it sends, using the php mail function
$success = true;
//set all the variables to blank to prevent re-submitting.
$cont_name = "";
$email = "";
$phone = "";
$hear_about = "";
$first_time = "";
} else {
$success = false;
}
} else {
$success = false;
}
}
?>
And the area not functioning correctly is
<fieldset>
<legend>How did you hear about us? <span class="phpformerror"><?php echo $errorhear_about; ?></span></legend>
<div><input type="checkbox" name="hear_about[]" value="Web" /> Web</div>
<div><input type="checkbox" name="hear_about[]" value="Newspaper" /> Newspaper</div>
<div><input type="checkbox" name="hear_about[]" value="Radio" /> Radio</div>
<div><input type="checkbox" name="hear_about[]" value="Driving" /> Driving Past</div>
<div><input type="checkbox" name="hear_about[]" value="Referal" /> Referal</div>
<div><input type="checkbox" name="hear_about[]" value="Other" /> Other</div>
</fieldset>
At the moment it will only come through displaying one of the variables if multiple variables are selected.
hear_about is an array and filter_var() does not handle arrays correctly. Instead use filter_var_array():
$hear_about = filter_var_array($_POST['hear_about'], FILTER_SANITIZE_STRING);
Remember that $hear_about is an array, and must be treated like one throughout your code (e.g. just using $hear_about won't work, it needs to be $hear_about[0], $hear_about[1], etc).
So for example in your trim line you would need something like:
foreach($hear_about as $key => $value) {
$trimhear_about[$key] = trim($value);
if(empty($trimhear_about[$key])){
//the hear_about field is empty
$errors = 1;
$errorhear_about[$key] = "This field is empty";
}
}
This will preserve the benefits of dealing with an array.
$_POST['hear_about'] is an array of values. You are handling it as a simple string!
I think you can solve simply replacing the line:
$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING);
With:
$hear_about = filter_var(implode(', ', $_POST['hear_about']), FILTER_SANITIZE_STRING);
The implode function (doc) "transform" an array to a string by concatenating the array values with the given glue. So you can just concatenate selected "How did you hear about us?" options with a comma and then use the resulting string as the other data.

Categories