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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Two languages for one form...

    Hello All.

    I am making a project that involves PHP and Javascript. I have one text field and a button that uses 'onclick = "go()", only today do i want to at PHP
    in to my program, i have changed my file to a .php and ready, i have apache and all that and know how to use PHP and have successfully used it before!
    So, i open my project through localhost, still no PHP code yet. works fine. soon i open php tags (<?php xxxx?>).........works fine. now, i then wanted to use
    the '$_POST' function to get the text field, the text field and button are not in a form, but i know for PHP i need it to be in a form, so i open form tags with a method of POST, give the texfield a name not just a id (PHP needs a name),
    this is when the javascript goes weird and the PHP is working, so lets say i type 'hello' it is meant to return hi (thats made in JS), but returns hi for one second before redirecting the page to the same page (because its now a form)........So, my problem as you can see is php works but when i print from javascript (.innerHTML) it shows for a second before the action of the form reloads the page. Sorry id i have not explained it very well, please ask if i am not giving enough info, Many Thanks to anyone who can help me out, that would be highly appreciated!!!!

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    We would need to see some code, but my psychic powers tell me that you're typing 'hello' and pressing the Enter key - which causes the form submission.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
     <?php





    $textfield 
    $_POST['textfield'];
     if (isset(
    $textfield) &&!empty($textfield)){
         
         
         if(
    preg_match('/yes/' $textfield)){
         
                 
                 echo 
    'OK';
                      
             }
     }     



    ?>




    <html>

    <head>

    <script>


    function go(){

    if (document.getElementById("textfield").value.substring(0, 2) === "ok"){
            
                  body2.innerHTML =  "yes"; 

     }


    };
    </script>
    </head>

    <body>



    <form action = "untitled.php" method = "POST">

    <input type = "text" name = "textfield" id = "textfield"><br>


    <input type = "submit" onclick = "go()">

    </form>



    <p id = "body2"></p>

    ok so theres the code, when you type 'ok' javascript will print 'yes' for as long until it reloads, PHP works when you type 'yes' it will print 'OK' and stay there, what i want is both to stay there!!!! Thanks

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,032
    Thanks
    15
    Thanked 240 Times in 240 Posts
    You can return data you submitted in your form to the same page by echoing it into the html or the javascript. You need an if( !isset ( not isset ) statement to provide any default values to use before the form is posted, otherwise you get errors.

  • #5
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, i don't quite understand (sorry), my problem is the javascript prints out data before the page reloads, this leads to the string it did print go again in seconds, but the php prints it on the page once its loaded, so it remains showing......why can't i do this in JS??

    thanks

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    <input type = "submit" onclick = "go()">
    This submit button will run your JS go() and then immediately submit the form.. that is the purpose of this button

    You could have a separate button to run your JS

    Code:
    <button id="justJS" onclick="go()">Go!</button>
    Alternatively,

    Code:
    <form action = "untitled.php" method = "POST" onsubmit="go()">
    will run your go() function on clicking the submit button, and will NOT submit if your function has 'return false;'.

    Added: To complete this list of choices, you could remove the submit button and use just

    Code:
    <button id="justJS" onclick="go()">Go!</button>
    and this go() function could, optionally, use

    Code:
    document.getElementById('yourformsid').submit();
    to submit the form.
    Last edited by AndrewGSW; 08-06-2012 at 01:09 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,032
    Thanks
    15
    Thanked 240 Times in 240 Posts
    It's better to use onmousedown instead of onclick for a submit button, because the form submits onmouseup. That is, if you want to run the javascript before the form submits.

  • #8
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you both, so i tried AndrewGSW's options but still 'yes' is only displayed before the page reloads (second), but maybe i mean, after the page reloads, THEN run the go() function, surly then the page would not reset the javascript because the javascript is only there until the page has finished reseting....So what would the code look like for that??


    Thanks to both, its really helpful!

  • #9
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    but maybe i mean, after the page reloads, THEN run the go() function
    I think you need to clarify in your own mind what you want to happen.

    It is possible to create PHP that causes some JS to run after the page has finished loading, but it is usually not necessary as the PHP itself can, for example, insert the word 'yes' into the page.

    But running some JS just prior to submitting/ reloading the page is a little pointless. The only reason to do so is usually to validate, or change, the form data and optionally cancel the submission of the form (if the data is invalid).
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #10
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, the code i posted is the bases of what i am doing, the reason why i want to languages is because i want to use PHP foe setting cookies, anyway,

    It is possible to create PHP that causes some JS to run after the page has finished loading

    How would i do that, or what is the code for what i need?

    Thank you very much!

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    echo "<script type=\"text/javascript\">window.onload=go;</script>;"
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #12
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sooo:


    Code:
     <?php
    
    
    
    
    
    $textfield = $_POST['textfield'];
     if (isset($textfield) &&!empty($textfield)){
     	
     	
     	echo "<script type=\"text/javascript\">window.onload=go();</script>"; 
     	
     	if(preg_match('/yes/' , $textfield)){
     	
     			
     			echo 'OK';
     	 			
     	 		 
    
     		}
     		
     } 	
    
    
    ?>
    
    
    
    
    <html>
    
    <head>
    
    <script>
    
    function go(){
    if (document.getElementById("textfield").value.substring(0, 2) === "ok"){
    		
    	body1.innerHTML = "You: " + document.getElementById("textfield").value;
    	 	 body2.innerHTML = "Paul: " + reply34; 
    
    }
    
    }
    </script>
    </head>
    
    <body>
    
    
    <form action = "untitled.php" method = "POST" >
    
    <input type = "text" name = "textfield" id = "textfield"><br>
    
    <button id="textfield" onclick="go()">Go!</button>
    
    </form>
    
    
    
    <p id = "body2"></p>


    Is not working....

  • #13
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Somehow in copying my code you've managed to add brackets after the word go..

    And where is reply34 defined?
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #14
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Correction:

    Code:
    <?php
    
    
    
    
    
    $textfield = $_POST['textfield'];
     if (isset($textfield) &&!empty($textfield)){
     	
     	
     	echo "<script type=\"text/javascript\">window.onload=go;</script>"; 
     	
     	if(preg_match('/yes/' , $textfield)){
     	
     			
     			echo 'OK';
     	 			
     	 		 
    
     		}
     		
     } 	
    
    
    ?>
    
    
    
    
    <html>
    
    <head>
    
    <script>
    
    function go(){
    if (document.getElementById("textfield").value.substring(0, 2) === "ok"){
    		
    	
    	 	 body2.innerHTML = "YES "; 
    
    }
    
    }
    </script>
    </head>
    
    <body>
    
    
    <form action = "untitled.php" method = "POST" >
    
    <input type = "text" name = "textfield" id = "textfield"><br>
    
    <button id="textfield" onclick="go()">Go!</button>
    
    </form>
    
    
    
    <p id = "body2"></p>

    .......Is not working.........


    (Sorry about that!!)

  • #15
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    body2.innerHTML = "YES ";
    if you haven't already defined body2 elsewhere then:

    Code:
    document.getElementById('body2').innerHTML = "YES ";
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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