Jump to content

Arbu

Members
  • Posts

    45
  • Joined

  • Last visited

Posts posted by Arbu

  1. I'm sending some information to the server with AJAX:

    function saveFileToAccount(filename){
    			/** [Storing JSON representations of the canvas] */
                    var data = formattedJSON();
                    /** [Store preview of transaction diagram] */
                    var image = canvas.toSVG();
    	
    				var xmlhttp = new XMLHttpRequest();
    			   
    			      var sendstring = "filename=" + filename + "&json=" + data + "&image=" + image;
    			      xmlhttp.open("POST", "includes/process_save.php", true);
    			      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    			      xmlhttp.send(sendstring);
    	
    		}

    This goes into an SQL database and I get it back with php when a new page is opened. But the data comes back truncated at the first ampersand that it contains. How do I fix this? I suppose I could convert all the ampersands into a little used character before saving the data and then convert them back when it comes back, but that doesn't seem very satisfactory. Is there a proper way to sort this out?

    Thanks.

  2. OK, setting my display items for the login status wasn't too hard. I don't need an AJAX request to find out; I can just set a variable with php on loading the page. 

    The problem I'm running into now is that, if the user enters the wrong login details I'd like the login form to remain in place so that I can give him a notification to that effect and he can try to enter in again. But by the time the code gets to hashandlogin, the login form has already gone. I tried removing data-iziModal-close from the submit button and closing the loginform manually if the login details were correct, but that really messed things up. 

    Any ideas? Maybe I should simply redisplay the login form with the relevant message instead of trying to get it to stay in place.

  3. At the moment I have the following. But I need to update the page to show whether the user is logged in (a) on loading and (b) when he logs in. So I'm thinking that I need to make an AJAX request in $(document).ready to see if he is logged in, then have a js function to set the details on the page that I can call from there and also when he logs in and out. I don't know if there's some way to have php simply send the log in details when the page is loaded.

    <?php if (login_check($mysqli) == true) : ?> 
            						<p>Welcome <?php echo ucwords(htmlentities($_SESSION['username'])); ?>!</p>
                        </div>
                        <a href="account.php"><div class="userLogin tooltip" title="Your account">  
              						<img src="images/icons/user.png" alt="" class="iconUser">
              					</div></a>
                        <a href="includes/process_logout.php">
              						<div style="margin-right: 15px;" class="userLogin tooltip" title="Logout">
              							<img src="images/icons/logout.png" alt="" class="iconUser">
              						</div>
              					</a>
                      <?php else : ?>
            						<p>You are not signed in.</p>
                        </div>
                        <div class="userLogin tooltip" title="Login or Sign Up" data-izimodal-open="loginform" data-izimodal-transitionin="fadeInDown">
              						<img src="images/icons/user.png" alt="" class="iconUser">
              					</div>
            		<?php endif; ?>

     

  4. OK, thanks very much, it seems to work OK now. I've gone with using POST rather than GET because I wasn't sure if I'd need to change INPUT_POST if I used GET. Hopefully sorting out the display at the browser end should be reasonably straightforward now.

    		function hashandlogin(a, b) {
    		     formhash(a, b);
    		    var xmlhttp = new XMLHttpRequest();
    		    xmlhttp.onreadystatechange = function () {
    		        if (this.readyState == 4 && this.status == 200) {
    		            console.log(this.responseText);
    		        }
    		    };
    			var sendstring="email=" +document.getElementById("login_email").value + "&p="+document.getElementById("hashedp").value;
    
    		    xmlhttp.open("POST", "includes/process_login.php", true);
    		    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    			xmlhttp.send(sendstring); 
    		}

     

  5. OK, good point, I hadn't noticed that that was there. So I've taken it out and now the page doesn't refresh. Hooray!

    The next thing is to get the data that I send to the server to be recognised. Naturally this doesn't work first time. I don't think that the server is actually finding the data that I send. Here's my code using the post method:

    function hashandlogin(a, b) {
    		     formhash(a, b);
    		    var xmlhttp = new XMLHttpRequest();
    		    xmlhttp.onreadystatechange = function () {
    		        if (this.readyState == 4 && this.status == 200) {
    		            console.log(this.responseText);
    		        }
    		    };
    			
    		    xmlhttp.open("POST", "includes/process_login.php", true);
    		    xmlhttp.send("email=" +document.getElementById("login_email").value + "&p="+document.getElementById("hashedp").value); 
    			
    		}

    and

    <?php
    
    include_once 'db_connect.php';
    include_once 'functions.php';
    
    sec_session_start(); // Our custom secure way of starting a PHP session.
    
    echo($_POST['email'] + 'abc');
    exit();

    The responseText is 0. What am I doing wrong?

    Edit: OK, so string concatenation in php is just done with a ".".  But I'm still not getting the email setting through, just the abc. Same if I use GET and put the data after the url.

     

  6. 1 hour ago, dsonesuk said:

    ALL input and button using type="submit" clicking will trigger submit event on form element, even while in input and hitting enter will cause a submission of form. So ALL! YES! I MEAN ALL! inputs data etc you wish to gather and calling of functions MUST! BE INITIATED after event.preventDefault() within the curly braces where that preventDefault() is placed. Where you then gather ALL the data you wish to send to php file using AJAX.

    Yes, but as I say, regardless of whether I have type=submit or not, the event.preventDefault never gets triggered.  If I can fix that I'll have a go at putting my AJAX code in the curly braces with that. But otherwise there's no point in trying that.

  7. OK here's my code now. I believe there should be a question mark after process_login.php not a full stop as you have it. Anyway I'm getting told that the post data is not set correctly.

      <?php if (login_check($mysqli) == false) :  ?>
             <div id="loginform" data-iziModal-group="grupo1">
                 <button data-iziModal-close class="icon-close">x</button>  <!--watch out here - JSFormat formatted the izimodal spec to have spaces in it and the button no longer worked.-->
                 <header>
                 <a href="" id="signin">Sign in</a>
                 </header>
                 <section>
                 <form name="login_form" id="login_form">
                 <input type="text" placeholder="Email" name="email" class="login_email" id="login_email">
                 <input id="password" type="password" placeholder="Password" name="password" class="login_password">
                 <footer>
                 <button data-iziModal-close>Cancel</button>
                 <button class="submit" data-iziModal-close onclick="hashandlogin(this.form, this.form.password);">Log in</button>
                 <p style="text-align: center; color: #444; font-size: 16px; font-family: Lato; padding-top: 70px; font-weight: 500;">
                Don't have an account? <a style="font-size: 16px; font-weight: 500; font-family: Lato;" href="register.php">Register here.</a></p>
                                </footer>
                                </form>
                            	</section>
                            </div>
    				
    			  <script type="text/javascript">
    			  
    			  document.getElementById("login_form").addEventListener("submit", function (event) {
    			      event.preventDefault()
    			  });
    			  
    		function hashandlogin(a, b) {
    		    formhash(a, b);
    		    var xmlhttp = new XMLHttpRequest();
    		    xmlhttp.onreadystatechange = function () {
    		        if (this.readyState == 4 && this.status == 200) {
    		            console.log(this.responseText);
    		        }
    		    };
    		    xmlhttp.open("GET", "includes/process_login.php?email=" +document.getElementById("login_email").value + "&p="+document.getElementById("hashedp").value, true);
    		    xmlhttp.send();
    		}
    function formhash(a, b) {
        var c = document.createElement("input");
        a.appendChild(c), c.name = "p", c.type = "hidden", c.id="hashedp", c.value = hex_sha512(b.value), b.value = "", a.submit()
    }

     

  8. Should it be the "submit" event or the "onsubmit" event. In any case I can't ever get the event.preventDefault() to trigger. If I set a break point there it doesn't get caught. If I take out the onclick property then still it doesn't trigger. 

    I'll keep working on how to access the email and password from the form.

  9. Seems I have to take out the post method for the form or it still refreshes the page.

    Which means that I have to figure out how to send the login information to the server. I think I do it by adding the details at the end of the address in xlmhttp.open, although how I get these details in the first place doesn't look straightforward.

  10. OK, thanks. I understand what you are saying, but still this does seem inordinately difficult. What I suppose I must do is have a form that:

    a) has a type =submit button,

    b) does not have an action for the form, as I don't want a new page shown,

    c) has preventDefault applied as I don't want the current page refreshed. Hopefully this will still cause the data to be submitted by the form (?),

    d) calls an AJAX procedure to log in and get the info I need to set what I want on the page with javascript.

    The first problem I am getting is that this still causes the page to refresh. Do I need to convert my php file to a function, and if so how? Here's my latest code:

     <?php if (login_check($mysqli) == false) :  ?>
             <div id="loginform" data-iziModal-group="grupo1">
                 <button data-iziModal-close class="icon-close">x</button>  <!--watch out here - JSFormat formatted the izimodal spec to have spaces in it and the button no longer worked.-->
                 <header>
                 <a href="" id="signin">Sign in</a>
                 </header>
                 <section>
                 <form method="post" name="login_form" id="login_form">
                 <input type="text" placeholder="Email" name="email" class="login_email" id="login_email">
                 <input id="password" type="password" placeholder="Password" name="password" class="login_password">
                 <footer>
                 <button data-iziModal-close>Cancel</button>
                 <button class="submit" data-iziModal-close type="submit" onclick="hashandlogin(this.form, this.form.password);">Log in</button>
                 <p style="text-align: center; color: #444; font-size: 16px; font-family: Lato; padding-top: 70px; font-weight: 500;">
                Don't have an account? <a style="font-size: 16px; font-weight: 500; font-family: Lato;" href="register.php">Register here.</a></p>
                                </footer>
                                </form>
                            	</section>
                            </div>
    				
    			  <script type="text/javascript">
    			  
    			  document.getElementById("login_form").addEventListener("submit", function (event) {
    			      event.preventDefault()
    			  });
    			  
    		function hashandlogin(a, b) {
    		    formhash(a, b);
    		    var xmlhttp = new XMLHttpRequest();
    		    xmlhttp.onreadystatechange = function () {
    		        if (this.readyState == 4 && this.status == 200) {
    		            console.log("done");
    		        }
    		    };
    		    xmlhttp.open("GET", "process_login.php", true);
    		    xmlhttp.send();
    
    		}
    		</script>	
    <?php
    
    include_once 'db_connect.php';
    include_once 'functions.php';
    
    sec_session_start(); // Our custom secure way of starting a PHP session.
    
    if (isset($_POST['email'], $_POST['p'])) {
        $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
        $password = $_POST['p']; // The hashed password.
    
        if (login($email, $password, $mysqli) == true) {
            $_SESSION["email"] = $email;
            saveLogin($_SESSION['user_id'],$mysqli);
    		echo ("something");
            // Login success
          //  header("Location: ../account.php");  I don't want the account page shown just cos user has logged in. But what should go here? If nothing or index.php then get a blank page with process_login.php as the address.
            exit();
        } else {
            // Login failed
      //      header('Location: ../login.php?error=1');
            exit();
        }
    } else {
        // The correct POST variables were not sent to this page.
        header('Location: ../error.php?err=Could not process login');
        exit();
    }

     

  11. OK, so I have two things that happen when the user clicks the OK button in my form. The form's data gets hashed and the user is logged in. I don't want any new page to be directed to so I remove the form's action setting (which called the log in procedure) and I remove the type="submit" property from the button's html. And I create a new function to call the hashing and logging in together. So my code is below. It's plainly not correct as it generates loads of errors. How do I call (a) a javascript function and (b) a php function from some html code which is within a php condition?

         <!--Login Modal-->
         <?php if (login_check($mysqli) == false) :  ?>
             <div id="loginform" data-iziModal-group="grupo1">
                 <button data-iziModal-close class="icon-close">x</button>  
                 <header>
                 <a href="" id="signin">Sign in</a>
                 </header>
                 <section>
                 <form method="post" name="login_form" id="login_form">
                 <input type="text" placeholder="Email" name="email" class="login_email" id="login_email">
                 <input id="password" type="password" placeholder="Password" name="password" class="login_password">
                 <footer>
                 <button data-iziModal-close>Cancel</button>
                 <button class="submit" data-iziModal-close onclick="hashandlogin(this.form, this.form.password);">Log in</button>
                 <p style="text-align: center; color: #444; font-size: 16px; font-family: Lato; padding-top: 70px; font-weight: 500;">
                Don't have an account? <a style="font-size: 16px; font-weight: 500; font-family: Lato;" href="register.php">Register here.</a></p>
                                </footer>
                                </form>
                            	</section>
                            </div>
    				
    			  <script type="text/javascript">
    		function hashandlogin(a,b){
    				formhash(a,b);
    				include_once 'process_login.php';
    				console.log(login_check($mysqli));
    		}	
    		</script>	
    			<?php endif; ?>		

     

  12. I don't mind using javascript. But if I understand correctly, an html form can only submit data by specification of a web page to be navigated to. That can be the current one, but it will be refreshed. 

    The user's login details will have to be stored on the server. Why can I not simply send a request to the server and get a response without having to refresh the user's browser?

  13. Thanks very much, I'll have a read through.

    The code is from a library called fabricjs. The library isn't working in relation to a particular matter for me. So I have been looking at the code to try to figure out why.

    Why would someone produce an open source library and obfuscate it? Aren't the two things contradictory?

    I have asked the authors why it's not working. They helped me a bit but then have stopped responding.

  14. I've learnt javascript through w3schools and it seemed fine. But when I go to look at and try to understand some code I don't feel as if my learning has got me anywhere. Here's an example:

          enlivenObjects: function(t, e, n, s) {
                var o = []
                  , i = 0
                  , r = (t = t || []).length;
                function a() {
                    ++i === r && e && e(o.filter(function(t) {
                        return t
                    }))
                }
                r ? t.forEach(function(i, r) {
                    i && i.type ? fabric.util.getKlass(i.type, n).fromObject(i, function(t, e) {
                        e || (o[r] = t),
                        s && s(i, t, e),
                        a()
                    }) : a()
                }) : e && e(o)
            },

    So a few questions:

    1. Is the function declaration simply a different way to say function enlivenObjects(t,e,n,s){...}?

    2. what on earth does this mean "r = (t = t || []).length;"?

    3. Is "r ?" shorthand for "if r!== null"?

    4. What does "i && i.type" mean?

    5. And this "e || (o[r] = t),"? I do know that || means "or". How can you have a line of code like that? If someone says to me "If it's raining or snowing stay indoors" I understand. If they just say "raining or snowing" it's not an instruction. Computer code should be about giving instructions surely?

    Maybe someone can help me out.

    Thanks.

     

  15. I want to remove all the timestamp tags from the "My Places" file that Google Earth provides. This file is an xml file and is on my hard drive. The stamps look like "<gx:TimeStamp><when>2014-06-08T00:12:24Z</when></gx:TimeStamp>".There are 4,000 such tags, so doing it manually one by one is not an option. I can't see how I would do a search and replace on the file because that would  not remove the data in the middle.

    So it seems I need to use XML Dom. I've been reading through the tutorials on W3Schools. Somehow I need to open the My Places file with the XMLHttpRequest object. How do I do that?

    Thanks.

×
×
  • Create New...