Solved

PHP Script - Am I missing anything here?

Posted on 2016-11-08
8
28 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
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 251 total points
Comment Utility
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
 
LVL 28

Assisted Solution

by:chilternPC
chilternPC earned 83 total points
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 83 total points
Comment Utility
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 2

Assisted Solution

by:Mike in IT
Mike in IT earned 83 total points
Comment Utility
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
Comment Utility
Turned out to be a user error! But thank you for the input!
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now