Using jquery var inside php

It’s s simple issue Im sure, but just need some advice on it, as i dont think its as simple as I have it below.

var email = $("input[name=\"email\"]").val();

Then call that in php

function emailFunction(){
<?php
$to = email;

mail($to, $subject, $content, $headers);
?>
};

The default mail function tends to fail a lot. So it’s better off using PHPMailer. Also what are you trying to achieve by nesting it inside a Javascript function? It makes simply no sense to me at all.

OK this is how that function is being called

if (data == 1){
swal({
title: 'You Are Registered!',
text: "Do you wish to continue to Log In?",
type: 'success',
success: emailFunction(),
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Continue'
}).then(function() {
window.location.href = "logIn.php";
})

You still should not be doing it in Javascript. I am guessing you didn’t learn from the other thread that you should NOT be using Javascript to do PHP stuff. If you want to email them, then do it in the login.php file. I believe you have the wrong idea how PHP works. What you are thinking is that PHP will only execute once the function is called. That is wrong. PHP will execute whether the function was triggered or not. So this logic is flawed.

Right I see ok, I am learning but its a day by day thing and this is all new to me, I havent got anyone sitting next to me or I can chat too so I’m trying to figure it all out as I go along.

I’ll look on google and see what’s best and learn from that, as I got so many projects going on with this at the moment, its hard to back track as there a deadline too

So reading it again on the success line, rather than calling the function, i call an external php page with the mail script in there, is that right?

And then post the email value to it.

Then I strongly suggest you DO NOT learn it from tutorials or anything that is more than 5 years old. When it comes to PHP, tutorials tend to not teach you anything about security. They will show you everything that is wrong, but never do it the right way.

The right place to learn is either a book that is up-to-date that complies with all types of security or a friend who generally knows the basics of PHP and security.


As per your question on your second reply. I am not entirely sure that’s how success works in Javascript. I maybe wrong because I don’t know Javascript as much as I know PHP.

I have it working, and I’ll post it below so just wondered if this is a better way of doing this.

function emailFunction(){
$.ajax({
url: 'register-mail.php',
type: 'POST',
data: {Email:email,},
});    
};

function onDataReceived(data)
{
if (data == 1){        
swal({
title: 'You Are Registered!',
text: "Do you wish to continue to Log In?",
type: 'success',
success: emailFunction(),
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Continue'
}).then(function() {
window.location.href = "logIn.php";
})
}
if (data == 2){
swal({
title: 'Error!',
text: 'This email address is already in use',
type: 'error',
confirmButtonText: 'Return'
})
}
};
}

register-mail.php

<?php 
if (!empty($_POST['Email'])){
$emailUser = $_POST['Email'];}else{header('Location: logIn.php');};
$to = $emailUser;
$from = "donotreply@site.com";
$headers = "From:" . $from;    
$subject = "Registration Complete";    
$content = "";
mail($to, $subject, $content, $headers);
?>;

It’s a little better than before, but you are missing so many things on your Javascript. I honestly don’t understand what you are trying to even do with Javsacript. Why make the login reliant on Javascript? When people have Javascript turned off, your login system won’t work for them.

1 Like

Ye I understand that now, and the good thing is that I have met the deadline and the management team are happy, so now I can go back and change things around behind the scenes and swap it over without anyone noticing.

Basically I started building a page for this company to showcase hundreds of hotels, it was only a pic, the name and the star rating, and there was three options to search for these hotels, by country, region and hotel name. So I designed a search in ajax that meant they could choose one of those from a radio button, then just type and the hotels appear as they type. everyone was happy, then I found out that this needed to work with the app that someone else was building, didnt know this and time was tight, so I took some of the skills Id learnt from that and put them into the register and log in pages.

The register form which we are talking about above, takes the visitors details and sends the details via a web service to the app’s database, if its a new email address its registered and i get a data value back, 1 or 0 depending on the result, and then all that above comes into play.

When I first found out I did it wrong not using php, I was neck deep in it, and when I looked at the php it didnt seems as easy so I stuck with it to get the project finished.

Then when they go to the login, the details they enter are checked again via the web service and if all good they proceed.

So where I think I’ve gone wrong is taking the registrants details and using jquery to talk to the web service instead of php.

But I really do appreciate your help and the advice I get here, its made a massive difference to my job

It’s not just that, there are tons of security issues with using Javascript in place of PHP. I would definitely say start over from scratch with somethings in mind.

Use standard practice. Don’t just Google something and the first thing that comes up, you use it as your final product. This is what a lazy developer does. I suggest if you really need that function and it’s a necessity, then do some research on that function.

  1. Make sure you are aware of the security flaws that it comes with.
  2. Make sure you understand what it essentially does. Don’t just assume what it does. To understand a function, you should create a testing file specifically for that function on a local server. DO NOT do it on a live server. You can potentially screw up your live server or create security holes.
  3. Make sure that your logic meets standard practices. Don’t use any legacy code/ logic. If you use something like if(isset($_POST['submit'])) then trash it and find a better solution, one which isn’t an amateur hack.

If you can’t start over from scratch, then I suggest removing all security flaw such as allowing users to manipulate Javascript so that data end up being tampered with.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.