Solved

PHP Script - Am I missing anything here?

Posted on 2016-11-08
8
61 Views
Last Modified: 2016-12-01
I've got a script in place that, depending on whether or not both email fields have been filled, the email code will send the email to both addresses. If not, it will only send it to the email address that was documented in just one of the fields.

It's been working fine, but there's a problem now (apparently), where if you put in a value in both email fields, the email will only be sent to the second address.

I've looked at it and I don't see anything logically that would explain that.

Here's the code:

Tell me if I'm missing anything:

$email_one=0;
$email_two=0;

if(isset($_POST['vendor_email_01'])&&!empty($_POST['vendor_email_01']))
{
$email_one=1;
}

if(isset($_POST['vendor_email_02'])&&!empty($_POST['vendor_email_02']))
{
$email_two=1;
}

if($email_one>0 AND $email_two==0)
{
$the_email = trim($_POST['vendor_email_01']);
}

if($email_two>0 AND $email_one==0)
{
$the_email = trim($_POST['vendor_email_02']);
}

if($email_one>0 AND $email_two>0)
{
$the_email = trim($_POST['vendor_email_01']).', '.trim($_POST['vendor_email_02']);
}

$the_id = $id;
$the_student_id = $student_id;
$email = $the_email;
$from_email = trim($_POST['rainmaker_email']);
$student_name = stripslashes($student_name);
$subject_line = "Credibility Index for ";
$subject_line .= $student_name;

$michelle = "update credibility set email_to = '$email', email_from='$from_email' where id = '$the_id'";
$michelle_query = mysqli_query($cxn, $michelle);
if(!$michelle_query)
{
$crap = mysqli_errno($cxn).': '.mysqli_error($cxn);
die($crap);
}
$to = $email;
$subject = $subject_line;
$from = $from_email;
$message .= "To view the credibility index for $student_name, either click on this link or copy and paste it into your browser: ";
$message .= "http://www.therainmakercompanies.com/RAP/adm/credibility_vendor_display.php?id=";
$message.=$the_id;
$message.=" Thanks!";
if (mail($to, $from, $subject, $message, $headers)) 
{
$good_email=1;
} 

Open in new window

0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41879854
try debug...

...
echo "the_email = ".$the_email;
exit();

$the_id = $id;
$the_student_id = $student_id;
...

Open in new window


what you get for the "the_email" here?
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 41879864
I think you are reusing the same variable.    What about simplifying your code?


$emails=[]; // set up blank array

// if email one is valid, add to emails array
if (veryifyEmail($_POST['vendor_email_01']){
	array_push($emails, $_POST['vendor_email_01']);
}

// if email two is valid, add to emails array
if (veryifyEmail($_POST['vendor_email_02']){
	array_push($emails, $_POST['vendor_email_02']);
}

// test if emails array has a value

if (empty($emails)) {
	die("No Emails In Array");
}

foreach ($emails as &$email) {
    // send email
}

// function to test valid email
function verifyEmal($email){
	if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
		return 1; 
	} else {
		return 0;
	}	
}

Open in new window

0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 251 total points
ID: 41879965
You can try this.
Use a ternary expression to test existence and validate in one go. The filter_var won't fire if the first expression in the AND fails so the expression will work even if vendor_email_0X is not defined.
We assume there is a valid email in the second and append a comma to the first if valid - then just trim any extra commas at the end.
<?php
if ($_POST) {
  $email = isset($_POST['vendor_email_01']) && filter_var($_POST['vendor_email_01'], FILTER_VALIDATE_EMAIL) 
    ? $_POST['vendor_email_01'] . ','
    : '';
  $email .= isset($_POST['vendor_email_02']) && filter_var($_POST['vendor_email_02'], FILTER_VALIDATE_EMAIL) 
    ? $_POST['vendor_email_02'] 
    : '';
  trim($email, ',');
  echo $email;
}
?>
<form method="post">
  <input type="text" name="vendor_email_01" placeholder="Email 01" /><br/>
  <input type="text" name="vendor_email_02" placeholder="Email 02" />
  <button type="submit">Submit</button>
</form>

Open in new window

Working sample here

EDIT: Updated sample url
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 29

Assisted Solution

by:chilternPC
chilternPC earned 83 total points
ID: 41880078
I think you should  actually debug your code instead of replace it with other code.  Personally, I couldn't replace  code without knowing why this code is failing.

I think the line :

if(isset($_POST['vendor_email_01'])&&!empty($_POST['vendor_email_01']))
{
$email_one=1;
}

must be failing  as all the other code is set  or cleared there in the code block.
I would add echo statements everywhere  to see what is actually being read in and set .
also just for testing,  hard set email_one =1 at the beginning to see what happens.

also I would change  the tests      "$email_two>0"  and "$email_one>0"   to  "$email_two=1"  and "$email_one=1"    on lines 14,19,24
1
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 83 total points
ID: 41880338
Please use var_export($_POST) right at the top of the PHP script and post the output here in a code snippet, thanks.

Also, you can simplify this expression...
if(isset($_POST['vendor_email_01'])&&!empty($_POST['vendor_email_01']))

Open in new window

... with something like this, because empty() covers the !isset() condition...
if( !empty($_POST['vendor_email_01']) ){

Open in new window

0
 
LVL 8

Assisted Solution

by:Mike in IT
Mike in IT earned 83 total points
ID: 41880469
You need to make sure that what is being sent to this page is two emails. Trying some debugging and
print_r($_POST)

Open in new window

to see what is getting to the page.

If you are not getting both emails then your problem is in the form where they are supposed to be sent from. If you are getting both then you will need to continue debugging till you find where the fault is. You can try placing the
print_r()

Open in new window

inside each
if

Open in new window

statement to see whether it is getting into the correct one.

Also remember that with the way you have your logic in your
if

Open in new window

statements if some how more than one of them would be true then your variable
$the_email

Open in new window

will be over written. You could modify them to
if . .  elseif . . . else

Open in new window

so that only one could ever be used.

You'll also want to make sure that all of your other variables that are coming over from your form are being sent. You can use an
echo

Open in new window

or more
print_r

Open in new window

or even
var_dump

Open in new window

to see what is in each of the variables.
0
 

Author Closing Comment

by:brucegust
ID: 41909407
Turned out to be a user error! But thank you for the input!
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41909757
You are welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question