I know this might be something I am missing on my side.
I am working on PHP, since I am new to it, I am facing a problem with accessing the variables from one function to another in the same class.
I am having a page called function.php and declared the variable globally.
This is my code
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
include 'Config.php';
/**
*
*
*/
class function
{
public $insert = array();
public function store()
{
$db = new Config();
$insert = array(
'date' => mysqli_real_escape_string($db->connection, $_POST['Date']) ,
'Tov' => mysqli_real_escape_string($db->connection, $_POST['Tov']) ,
'Name' => mysqli_real_escape_string($db->connection, $_POST['Name']) ,
'rh_first' => mysqli_real_escape_string($db->connection, $_POST['rh_first']) ,
'se_second' => mysqli_real_escape_string($db->connection, $_POST['se_second']) ,
'pk_third' => mysqli_real_escape_string($db->connection, $_POST['pk_third'])
);
return $insert;
}
public function Send()
{
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(); // Passing `true` enables exceptions
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxx@gmail.com'; // SMTP username
$mail->Password = 'yyyy'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
if ($this->insert['rh_first'] == "P") {
//Recipients
$mail->setFrom('xxx@gmail.com', 'xxx');
$mail->addAddress('xvb@gmail.com', 'xvb');
//content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test';
$mail->Body = 'Test email';
}
//Recipients
$mail->setFrom('xxx@gmail.com', 'xxx');
$mail->addAddress('yyyya@gmail.com', 'yyyya');
// $mail->addCC('asdfg@gmail.com');
//Attachments
$mail->addAttachment('lobbyq.png', 'sample.png'); // Add attachments
// Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test2';
$mail->Body = 'Test2 messagwe';
if (!$mail->send()) {;
return "Message not sent" . $mail->ErrorInfo;
} else {
return "email send";
}
}
}
The above code contain a function to store and send email via phpmailer
There are six fields to store in database (on Function store()) where last three are radio buttons.
When I tried to run the code got an error as Undefined index 'rh_first'
as I didn't initialize it in global.But i did as $insert = array();
globally.
Please suggest me a better way to access the key of the array variable into another function of the same class.
This function needs to set the value of $this->insert
:
public function store()
{
$db = new Config();
$insert = array(
'date' => mysqli_real_escape_string($db->connection, $_POST['Date']) ,
'Tov' => mysqli_real_escape_string($db->connection, $_POST['Tov']) ,
'Name' => mysqli_real_escape_string($db->connection, $_POST['Name']) ,
'rh_first' => mysqli_real_escape_string($db->connection, $_POST['rh_first']) ,
'se_second' => mysqli_real_escape_string($db->connection, $_POST['se_second']) ,
'pk_third' => mysqli_real_escape_string($db->connection, $_POST['pk_third'])
);
$this->insert = $insert;
return $insert;
}
Notice the new line: $this->insert = $insert;
?
This will set the property of your class to be the value of the $insert
array.
Also, I left the fact that it returns the value of $insert
, because I don't know what the rest of your code looks like, however, I think this function can just return void...
You have not defined a global variable, but a class variable. And if you want to access it within the class, you have to use $this->insert
instead of $insert
, as the second one would be a function-scoped variable.
Anyways, try to rework your code. From my point of view, these inserted values could better live in a function argument.
Finally, you should place the autoloader on the outermost level, just after the use
statements.
You can access the array using $this->insert
since it is declared globally.
To get key and value use:
foreach ($this->insert as $key => value) {
echo $value; // prints the values of insert
echo $key; // prints the keys of insert
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.