Page 1 of 1

Add id from php variable into javascript code

Posted: 17 Apr 2020, 05:13
by JanMolendijk
Dear Coursesweb about mail-function I try another way to complete
Now my problem is to get the referenceid from Url.

Code: Select all

<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function (e){
$("#frmEnquiry").on('submit',(function(e){
	e.preventDefault();
	$('#loader-icon').show();
	var valid;	
	valid = validateContact();
	if(valid) {
		$.ajax({
		url: "mail-send.php?id='.$_GET['referenceid'].'",
		type: "POST",
		data:  new FormData(this),
		contentType: false,
		cache: false,
		processData:false,
		success: function(data){
		$("#mail-status").html(data);
		$('#loader-icon').hide();
		},
		error: function(){} 	        
		
		});
	}
}));

function validateContact() {
	var valid = true;	
	$(".demoInputBox").css('background-color','');
	$(".info").html('');
	$("#userName").removeClass("invalid");
	$("#userEmail").removeClass("invalid");
	$("#subject").removeClass("invalid");
	$("#content").removeClass("invalid");
	
	if(!$("#userName").val()) {
		$("#userName").addClass("invalid");
        $("#userName").attr("title","Required");
        valid = false;
	}
    if(!$("#userEmail").val()) {
        $("#userEmail").addClass("invalid");
        $("#userEmail").attr("title","Required");
        valid = false;
    }
    if(!$("#userEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
        $("#userEmail").addClass("invalid");
        $("#userEmail").attr("title","Invalid Email");
        valid = false;
    }
	if(!$("#subject").val()) {
		$("#subject").addClass("invalid");
        $("#subject").attr("title","Required");
		valid = false;
	}
	if(!$("#content").val()) {
		$("#content").addClass("invalid");
        $("#content").attr("title","Required");
		valid = false;
	}
	
	return valid;
}

});
</script>
With url: "mail-send.php?id='.$_GET['referenceid'].'", I try to get the referenceid

After this maill-send page is loading into the same page
but their I can`t find out how to get the referenceid

Code: Select all

<?php 


$referenceid = isset($_GET['referenceid']) ?(int) $_GET['referenceid'] :'';


//your code ..
?>

<script>
var id_usr1 ='<?php echo $referenceid; ?>';
</script>

<?php 
  include('connection.php');

$sql=mysqli_query($conn,"SELECT * FROM `report` WHERE reference_id_2='$referenceid' ");
$profile=mysqli_fetch_assoc($sql);
?>




<?php
require('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Port     = 465;  
$mail->Username = "YOUR USER_NAME";
$mail->Password = "YOUR PASSWORD";
$mail->Host     = "YOUR HOST";
$mail->Mailer   = "smtp.kpnmail.nl";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("jan-molendijk@live.nl");	
$mail->Subject = $_POST["subject"];
$mail->WordWrap   = 80;
$mail->MsgHTML($_POST["content"]);

foreach ($_FILES["attachment"]["name"] as $k => $v) {
    $mail->AddAttachment( $_FILES["attachment"]["tmp_name"][$k], $_FILES["attachment"]["name"][$k] );
}

$mail->IsHTML(true);

if(!$mail->Send()) {
	echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
	echo "


okok '. $profile[userName] .'


<form action='' method='post'>
First Name: <input type='text' name='first_name'><br>
Last Name: <input type='text' name='last_name'><br>
Email: <input type='text' value=''. $profile[userName] .'' name='email'><br>
Message:<br><textarea rows='5' name='message' cols='30'></textarea><br>
<input type='submit' name='submit' value='Submit'>
</form>

";
}	
?>
I hope you could help me with this hope my question is clear enough ????

Add id from php variable into javascript code

Posted: 17 Apr 2020, 15:21
by Admin
Check the page source in browser to see if that id is added in the "url" address from the $.ajax function.
If it is added, apply an "echo" to the message that is added in the mail to see what data contains.

Add id from php variable into javascript code

Posted: 17 Apr 2020, 15:48
by JanMolendijk
I see only this result in the javascript without reference what can I do I realy don`t known

Code: Select all

url: "mail-send.php?referenceid=$_GET['referenceid']",

Add id from php variable into javascript code

Posted: 17 Apr 2020, 16:21
by Admin
That page must be loaded /generated via php.
Then, add with php "echo" that value in page code, like this:

Code: Select all

url: 'mail-send.php?referenceid=<?php echo $_GET['referenceid']; ?>',

Add id from php variable into javascript code

Posted: 18 Apr 2020, 03:43
by JanMolendijk
Ow sometimes I`m stupid :D thanks it is working now