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 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2016
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    jquery returning unwanted html code

    Hi there,
    I have a piece of javascript that logins in a user, it passes the form data to a php script that returns a value (either authenticated or not)
    however it appears to be adding extra html code <strong></strong> to the response string.

    In my php i have echo strip_tags("authenticated");
    my js is
    Code:
    event.preventDefault();
    	ajaxRequest= $.ajax({
                url: "/authenticate_user.php",
                type: "post",	
                data: {
    				s:'login-user-in',
    				username: $('#txtusername').val(),
    				password: $('#txtpassword').val(),
    				rememberme:rememberme
            }
            });
         ajaxRequest.done(function (response, textStatus, jqXHR){
              // show successfully for submit message
            // $("#resultmsg").html('Submitted successfully');
    		// alert(response);
    		 if(response ==='authenticated'){
    		 $("#msg").html(response);	
    		 }else{
    		 $("#msg").html(response);
    		}
         });
    
         /* On failure of request this function will be called  */
         ajaxRequest.fail(function (){
           // show error
    	$("#resultmsg").html('There is error while submit');
        // console.log('There is error while submit');
    	 });
    }

  2. #2
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    11,194
    Thanks
    6
    Thanked 1,345 Times in 1,314 Posts
    Got a live example for us? Or at least some more context? Perhaps that element is created somewhere else and existing even before the response is returned?

  3. #3
    New Coder
    Join Date
    Apr 2016
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok here is the php code that is called
    Code:
    $type = $_POST['s'];
    switch ($type){		
    	case 'login-user-in':
    	               $username = $_POST['username'];
    	               $pword = $_POST['password'];
    	               logUserIn($username,$pword);
    		break;		
    	default;
    		break;
    }
    
    function logUserIn($username,$password){
    	global $hadb;
    	$row = $hadb->getUserLoginDetails($username);
    	$salt =  $row['salt'];
    	$hash = $row['hashed'];
    	
    	if(password_verify($password . $salt, $hash)){
    	//password verified";	
    		echo strip_tags("authenticated");
    	}else{
    		echo strip_tags("Username or Password is Incorrect");
    	}
    
    }
    the previous JavaScript is what I use to sent the data to the file containing the function above.
    so where you see echo strip_tags("authenticated"); that is the expected response back to the javascript, instead i am getting <strong></strong>authenticated as the response.
    it is adding this extra strong tags from nowhere

    hope that helps

  4. #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,889
    Thanks
    58
    Thanked 693 Times in 688 Posts
    Quote Originally Posted by VIPStephan View Post
    Got a live example for us? Or at least some more context?
    I'm with VIPStephan here - there's nothing in the code shown that would add tags to your output so that must be happening somewhere else. If you can't show us a live example or your complete code we're just going to be stumbling around in the dark

    As a workaround, you could try
    Code:
    $("#msg").text(response);
    If the strong tags still show up you know it's a client side thing. If they don't, something else is going on in your output.

  5. #5
    New Coder
    Join Date
    Apr 2016
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hey folks figured it out, there was <strong><strong> tags at the beginning, before the <?php in my common functions php file that i have. no idea how it got there, i have removed it and all ok again.

    thanks

  6. #6
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,222
    Thanks
    4
    Thanked 470 Times in 458 Posts
    Little side note, you've got static text so why are you doing a striip_tags on it. Also IF you were to have tags in it you didn't want rendered, you should probably be doing htmlspecialchars instead of strip_tags.

    That or just send it regardless and use jQuery's .text method instead of .html -- or even BETTER kick the mental enfeeblement that is jQuery to the curb and create an actual textnode on the DOM.

    Code:
    var
    	x = new XMLHttpRequest(),
    	d = document, /* amazingly not only is this less code, it runs faster. */
    	txtUsername = d.getElementById('txtusername'),
    	txtPassword = d.getElementById('txtpassword'),
    	resultMsg = d.getElementById('resultmsg');
    x.onreadystatechange = function() {
    	if (x.readyState == 4) {
    		if (x.status == 200) resultMsg.appendChild(d.createTextNode(x.responseText));
    		else alert('There was an error during submit, HTTP Status code: ' . x.status);
    	}
    };
    x.open('POST', '/authenticate_user.php');
    x.send(
    	's=login-user-in&rememberme=rememberme&username=' .
    	encodeURIComponent(txtUsername.value) .
    	'&password=' . encodeURIComponent(txtPassword.value)
    );
    163 bytes less code, would execute faster with less overhead (and less endless ceaseless getting the same blasted elements over and over again as jQuery derps people into doing)... but tell me again how much time and effort all that jQuery garbage is saving you.
    Last edited by deathshadow; Oct 15th, 2018 at 02:23 AM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

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