Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Help with a small php problem

Wes.

Active Coder
Hi. I have recently moved my website to another host, but they dont do Classic ASP (yes I know it's old, so am I :)) so I am having to re-write the thing in HTML and PHP. Maybe someone can help me with a small but infuriating problem which I have spent two days trying to figure out. Here it is:
I have two variables, $A and $B each of which contains a randomly generated number. These numbers are displayed in a Form on the screen. Then I have a text Input tag where the user can add together the two numbers then type in the answer. The code then needs to compare the sum of the two numbers with the user answer and behave according to whether the answer is correct or not. The problem is I dont seems to be able get the user answer from the text input. When I submit the Form (method Post) the input just seems to disappear into the ether and I cant find it.
Here's the input
<input type="text" id="answer" name="user_answer" title="type your answer here" value=""/>

and here's the routine which I expected to store the user input in the variable $answer
if (empty($_POST["user_answer"])) {
$answerErr = "* Answer req'd";
} else {
$answer = test_input($_POST["user_answer"]);
}
}

Any help would be greatly appreciated

EDIT: I'm new here and when I was completing the registration form I noticed that it contains a Verification section which is exactly what I'm trying to do!
 
Well would you believe it? I come back to it this morning, having made no alterations to the code, and it works!!!!
I have had another similar situation since moving to this hosting company. There is an image on my homepage which at first just wouldn't load, even though the code snippet I was using was the same as for all the other images on the page. I spent ages trying this and that but in the end I gave up for the day. Then the next day I fired up the page and there was the image! It looks like I may have wasted your time on this forum and that this is an issue I should maybe take up with the hosts. Thanks for your interest anyway, I have a lot more work to do yet and I am a relative novice when it comes to PHP so I may be back ;-)
 
Well I said I would be back, it didn't take long :)
I am working on a contact form in PHP. It seems I have to have something called PHP Mailer installed somewhere but this is way out of my comfort zone and I really need someone to take me through it step by step. I have read the instructions on the internet but much of the terminology is unfamiliar to me and I am not able to get any further.
Anybody out there take pity on me?
 
Well I said I would be back, it didn't take long :)
I am working on a contact form in PHP. It seems I have to have something called PHP Mailer installed somewhere but this is way out of my comfort zone and I really need someone to take me through it step by step. I have read the instructions on the internet but much of the terminology is unfamiliar to me and I am not able to get any further.
Anybody out there take pity on me?
It may be worth starting a new thread for this new topic. I'm not 100% sure but that sounds like something that pretty much any webhost already does, so you can begin writing the functions you need to send an email.

I appreciate more than anybody about coding things for yourself, but maybe it's worth finding an existing contact form script and use that? You can also learn a lot from the code base.
 
Wes.,

You should be able to simply download the code of PHPMailer, include it to your PHP code and it should work right away.

If the folder of PHPMailer is in the same folder of your PHP file, you include it like such:
PHP:
require('PHPMailer/src/Exception.php');
require('PHPMailer/src/PHPMailer.php');
require('PHPMailer/src/SMTP.php');

Here you provide the information about the mail server and your email/password:
PHP:
$mailClient = new PHPMailer(true);
$mailClient->isSMTP();
$mailClient->Host = $mailServerAddress;
$mailClient->SMTPAuth = true;
$mailClient->SetFrom($mailAddress);
$mailClient->Username = $mailAddress;  
$mailClient->Password = $password;
$mailClient->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mailClient->Port = 587;

And you send a simple mail:
PHP:
try {
    $mailClient->addAddress($recipient);  

    $mailClient->Subject = $subject;
    $mailClient->Body = $body;

    $mailClient->send();

    return true; // Ok, it worked.
}
catch (Exception $e)
{
    return false; // Error.
}
 
That all looks so simple, I can more or less understand that right away. Why do they make it sound so complicated??? I will give it all a try and let you know how I get on :blush:
 
Hi. So far so good, however there is a problem somewhere, because the page wont load unless I comment out some (not all) of your lines of code, see below.
I dont know whether this has any bearing on it, but I'm not sure I've copied all the PHPMailer stuff to my webspace. I've got a folder called 'PHPMailer' containing another folder called 'src' in which are the three 'require' files referred to in your code, Exception.php, PHPMailer.php and SMTP.php.
However - I obtained PHPMailer from their website by downloading 'PHPMailer-master.zip'. When I unzipped it, there were a lot more files and folders beside the ones you referred to. Should I have copied all those over? and if so where to?
Below shows the commented out code:

$mailServerAddress = "montague.ldn.kualo.net";
$mailAddress = "[email protected]";
$password = "fA1na51te548l";

//$mailClient = new PHPMailer(true);
//$mailClient->isSMTP();
$mailClient->Host = $mailServerAd
dress;
$mailClient->SMTPAuth = true;
//$mailClient->SetFrom($mailAddress);
$mailClient->Username = $mailAddress;
$mailClient->Password = $password;
//$mailClient->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mailClient->Port = 465;

If I remove any of the '//' the page wont load. Thanks for your patience :)
 
I forgot PHPMailer uses namespaces. You need to write the following at the start of the PHP file:
PHP:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

You changed the following to fit the location of your PHPMailer location?:
PHP:
require('PHPMailer/src/Exception.php');
require('PHPMailer/src/PHPMailer.php');
require('PHPMailer/src/SMTP.php');
Personnaly, the "src" folder (In the picture you showed us) is located directly in the "PHPMailer" folder.
 
Hi thanks for your reply. Firstly the namespaces, just to be clear do you mean right at the top before anything else, or at the start of the PHP?
Secondly, yes the location of the files in my webspace is as shown, the picture is of the unzipped files on my HDD.
 
Hi thanks for your reply. Firstly the namespaces, just to be clear do you mean right at the top before anything else, or at the start of the PHP?
I would put them after the 'require's.

You should extract PHPMailer into the same folder as the PHP file that uses PHPMailer, rename the folder to 'PHPMailer' and make sure it contains a folder named 'src'.
 
Thanks, I'll get the hang of this yet... So as not to waste too much of your time let me get this clear.
The PHP file that uses PHPMailer is 'contact.php'
So :-
1. I should copy all the extracted files and folders (as shown in the picture) into the same directory as 'contact.php' ?
2. Then I create a folder named 'PHPMailer' in the same directory. ?
3. In that folder there should be another folder named 'src' which contains 'Exception.php', 'PHPMailer.php' and 'SMTP.php'. ?
 
Actually, it does not really matters where you extract PHPMailer. What matters is that when you want to include it to your project, you must tell PHP where PHPMailer is located.

Your 'require' should match the following:
PHP:
require('PATH_TO_THE_FOLDER_OF_PHPMAILER/src/Exception.php');

The folder 'src' (Which is located in the archive you downloaded) contains the code of PHPMailer.

Did it worked out? For me, it worked right off the bat. Is there any PHP error? Or is there no error but the mail is not received?
 
Last edited:

Latest posts

Buy us a coffee!

Back
Top Bottom