Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript problem with php, help me pls

    PHP Code:
    ?><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..?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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.

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can u show me? or how can i find the solution or another way to do it?

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    Using jquery u should do something like this.

    Code:
    <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

    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;
    	}


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •