Jquery variables are returning "Undefined"

Asked

Viewed 21 times

-1

I have this block of Jquery code that calls an ajax function and the variables when they are passed to php are returning "Undefined".

Here’s the Jquery code block I have:

var email = $('#signupEmail').val();
var password = $('#signupPassword').val();
var ConfirmPassword = $('#signupConfirmPassword').val();
var Plans = $('#plans option:selected').val();

console.log(email);
console.log(password);
console.log(ConfirmPassword);
console.log(Plans);

$.ajax({
    type: 'POST',
    url: 'enviarEmail.php',
    data: { email1:email,password1:password,ConfirmPassword1:ConfirmPassword,Plans1:Plans },
    success: function(response) {
         //alert('Transaction completed!! Check your email to confirm it and then login :D');
    }
});

console.log($('#phpVar1').val());
console.log($('#phpVar2').val());
console.log($('#phpVar3').val());
console.log($('#phpVar4').val());

This is all the code that is in the php file, I was doing tests trying to see if I could get the value of the variables that I was passing by parameter in ajax for php and the php file were not being received.

enviar Mail.php


<input type="hidden" id="phpVar1" value="<?php echo $_POST["email1"]; ?>">
<input type="hidden" id="phpVar2" value="<?php echo $_POST["password1"]; ?>">
<input type="hidden" id="phpVar3" value="<?php echo $_POST["ConfirmPassword1"]; ?>">
<input type="hidden" id="phpVar4" value="<?php echo $_POST["Plans1"]; ?>">

<?php
session_start();

/*
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once '../../assets/vendor/phpmailer/src/Exception.php';
require_once '../../assets/vendor/phpmailer/src/PHPMailer.php';
require_once '../../assets/vendor/phpmailer/src/SMTP.php';

$mail = new PHPMailer(true);
// Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // for detailed debug output
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;

$mail->Username = '[email protected]'; // YOUR gmail email
$mail->Password = 'Siqueira8_'; // YOUR gmail password

// Sender and recipient settings
$mail->setFrom('[email protected]', 'WatchWFun');
$mail->addAddress($_POST["email"], 'Receiver Name');
//$mail->addReplyTo('[email protected]', 'Sender Name'); // to set the reply to

// Setting the email content
$mail->IsHTML(true);
$mail->Subject = "Email Confirmation";
$mail->Body = '
Did you just create an account ? <br>
Please click this link to confirm your email.<br>
http://localhost/ProjetoFinal/html/home/index.php?opt=4&email='.base64_encode(@$_POST["email"]).'
';
$mail->send();
*/


//passar o valor dos campos para a função
/*
registo(
$_POST["email1"],
$_POST["password1"],
$_POST["ConfirmPassword1"],
$_POST["Plans1"],
$_POST["paymentMethods1"]);
*/

?>


And the error that Im getting is this:

[![inserir a descrição da imagem aqui][1]][1]

Na primeira vez que apresento as variáveis na consola elas retornam com os valores que pretendo mas quando passo para o php o valor de todas fica "undefined" como podemos ver a baixo. 

  [1]: https://i.stack.imgur.com/HQ0p7.png

  • hello and welcome Diogo, this is the OS website in Portuguese, translate your question

  • you are trying to read the result of php without waiting for it to be processed before, to read return, put the code inside the success, which is when the ajax call will get the answer

  • In this way they are also returning "Undefined".

  • put the full code in php, remembering that an ajax call does not receive a "page" so it can do $('#phpVar1').val(), this should be a normal post, an ajax should receive a return from php, which should for example return values using a echo, I think you are confusing there how to do and receive the return of an Ajax call, but to help more put all the php code

  • I edited the top, now you can see the whole php file

  • Hello Diogo, welcome to the site. It is important [Edit] and add a [mcve] of the problem (DO NOT delete and DO NOT repeat the question), with a step by step of what has already done and explain clearly and objectively and then wait for the reopening process (which will be evaluated by other users). To better enjoy the site, understand and avoid closures is worth reading the Stack Overflow Survival Guide in English. Thank you for understanding.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.