Add id from php variable into javascript code

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
Posts: 267
Location: Holland Rotterdam

Add id from php variable into javascript code

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){
	var valid;	
	valid = validateContact();
	if(valid) {
		url: "mail-send.php?id='.$_GET['referenceid'].'",
		type: "POST",
		data:  new FormData(this),
		contentType: false,
		cache: false,
		success: function(data){
		error: function(){} 	        

function validateContact() {
	var valid = true;	
	if(!$("#userName").val()) {
        valid = false;
    if(!$("#userEmail").val()) {
        valid = false;
    if(!$("#userEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
        $("#userEmail").attr("title","Invalid Email");
        valid = false;
	if(!$("#subject").val()) {
		valid = false;
	if(!$("#content").val()) {
		valid = false;
	return valid;

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


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

//your code ..

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


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


$mail = new PHPMailer();
$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   = "";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->Subject = $_POST["subject"];
$mail->WordWrap   = 80;

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


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'>

I hope you could help me with this hope my question is clear enough ????

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.

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']",

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']; ?>',

Ow sometimes I`m stupid :D thanks it is working now

Similar Topics