Solved

PHP Script - Am I missing anything here?

Posted on 2016-11-08
8
58 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 50

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 52

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 55

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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 28

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 109

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 7

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 55

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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