...

View Full Version : Javascript problem with php, help me pls



mya7709
01-02-2012, 09:16 AM
?><script type="text/javascript">
var answer=confirm("Do you want to update your email?");
if(answer==true)
{
<?php
mysql_query("update user set u_email ='$email' where u_user='$username'");
?>

alert ("You choose update your email.");

}
if(answer==false)
{
alert ("You had rejected to update your email.");
//answer=0;
}

</script> <?


i put this javascript within my php, it should perform update when i click ok, and perform nothing when i click cancel, but the problem is, it will still perform update although i click cancel. someone can solve this for me pls..?

devnull69
01-02-2012, 09:22 AM
This is a serious comprehension problem. You cannot mix javascript and PHP in that way and expect some PHP code to execute on a javascript condition. No way!

As soon as the javascript code executes, the PHP part has already finished and cannot interact with javascript like that.

If you want to start some PHP action from javascript you'll need to use some AJAX techniques.

mya7709
01-02-2012, 09:33 AM
can u show me? or how can i find the solution or another way to do it?

devnull69
01-02-2012, 02:51 PM
If you search for "Ajax" you will find a zillion examples all over the net. If you experience problems with your own code afterwards you can still come back here.

sorlaker
01-02-2012, 03:47 PM
Using jquery u should do something like this.



<script type="text/javascript">
var answer=confirm("Do you want to update your email?");
if(answer==true)
{
$.post('confirm_email.php', function (data){
// var data will show 1 = true; or ; 0 = false
// in case of success
var result = parseInt(data);
if (data){
alert ("You choose update your email.");
}else{
alert("Error");
}
});
}
if(answer==false)
{
alert ("You had rejected to update your email.");
//answer=0;
}
</script>



and this is the confirm_email.php code



//get user email and username using something like COOKIES
$email = $_POST['email'];
$username = $_POST['username'];
mysql_connect('hostname', 'user', 'pass');
mysql_select_db('db name');
if (mysql_query("UPDATE `user` SET `u_email` = '$email' WHERE u_user = '$username'")){
//success
echo 1;
}else{
//error
echo 0;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum