Solved

PHP Script - Am I missing anything here?

Posted on 2016-11-08
8
53 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
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 52

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 6

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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
Form not operating correctly. 1 22
only allow numbers with preg match 4 25
updating table data with inner join 9 24
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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 …

912 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

18 Experts available now in Live!

Get 1:1 Help Now