SlideShare a Scribd company logo
Send, pass, get variables with PHP, Form, HTML & JavaScript code                     http://www.skytopia.com/project/articles/compsci/form.html




         Skytopia > Projects > Technology/science/misc articles > HTML Forms in a nutshell

                                                                   Ads by Google

                                                                   PHP CMS Script
                                                                   PHP Form Post
                                                                   PHP


                     Super useful bits of PHP, Form and JavaScript code
                                      All code on this page is considered by us to contain the most basic and important aspects of form
             SpeedXML                 sending. This page basically shows in the shortest possible way how you can:
           Allows to extract
              import XML               Test your PHP offline - Test your PHP pages offline easily, without installing your
               Data Free
           evaluation copy
                                      own web server.
                on line
           www.idellys.com/soluti       Send variables between Javascript, Form and PHP on the same page:

                                      * PHP variable to Javascript variable or send Form variable to Javascript variable
                                      * PHP variable to Form variable or send Javascript variable to Form variable
                                      * Javascript variable to PHP variable (impossible) or send Form variable to PHP variable (impossible)

           Send variables from one page to another:

               * URL variables to PHP/JavaScript page - Allow sending different variables via the URL string to the new page (readable
               through PHP or Javascript). This is more appropriate than the below if there are few pieces of small info you wish to
               send.
               * PHP variables to PHP page - Alternatively, maintain a session by sending PHP variables to another page with PHP in
               (allows super large arrays).
               * Form variables to PHP page - Alternatively, allow sending hidden form variables to a PHP page. This allows the user to
               (more easily than the above methods) select options on the page (via URL, radio buttons, or dropdown menu), and for these to be sent
               to the new page.
               * Form variables (tick boxes) to PHP page - As above but with tick boxes.

           Dynamically update HTML - Allow different bits of HTML to dynamically execute according to user
         input:

               * Change HTML according to radio button selection.
               * Change HTML according to dropbox selection.
               * Add/remove bits of HTML according to multiple checkboxes.




         Send variables between Javascript, Form and PHP on the same page:
         PHP variable to Javascript variable:
                       <?php        $myvar=10;   ?>

                       <script type="text/javascript">
                               jsvar = <?php echo $myvar; ?>;
                               document.write(jsvar); // Test to see if its prints 10:
                       </script>




1 of 6                                                                                                                                   16-Jan-12 9:58 PM
Send, pass, get variables with PHP, Form, HTML & JavaScript code                http://www.skytopia.com/project/articles/compsci/form.html


         Form variable to Javascript variable:
                  <form name="myform4">        <input type="hidden" name="formvar" value="100">              </form>

                  <script type="text/javascript">
                          jsvar = document.myform4.formvar.value;
                          document.write(jsvar) // test
                  </script>

         PHP variable to Form variable:
                  <form name="myform4">
                          <input type="hidden" name="formvar" value="<?php $phpvar=10; echo $phpvar; ?>"> // PHP code inside HTML!!
                  </form>

         Javascript variable to Form variable:
                  <form name="myform3">
                          <!-- It needn't be a "hidden" type, but anything from radio buttons to check boxes -->
                          <input type="hidden" name="formvar" value="">
                  </form>

                  <script type="text/javascript">
                          jsvar=10;
                          document.myform3.formvar.value = jsvar;
                  </script>

         Javascript variable to PHP variable:

         This is impossible (unless you reload the page, or call another page), since all PHP code is rendered first (on the server side),
         and then Javascript is dealt with afterwards (on the client side).


         Form variable to PHP variable:

         Without refreshing, this is impossible like above for similar reasons. If you don't mind a refresh, then either a page reload or
         calling another web page would work if you used something like $_POST['FormVar']; in the php file.




         Allow sending different variables via the URL string to the new page (readable
         through PHP or Javascript).
         The version shown below uses PHP to read the variables back, but it's possible to use Javascript and some messy splitting to
         do the same (for that route, see this nice little function, or here, or here for example code.)

         EXAMPLE:

         Send variables via URL!

         INSIDE "page1.php" or "page1.html"
                  // Send the variables myNumber=1 and myFruit="orange" to the new PHP page...
                  <a href="page2c.php?myNumber=1&myFruit=orange">Send variables via URL!</a>




         INSIDE "page2c.php"
                  <?php
                           // Retrieve the URL variables (using PHP).
                           $num = $_GET['myNumber'];
                           $fruit = $_GET['myFruit'];
                           echo "Number: ".$num." Fruit: ".$fruit;
                  ?>




2 of 6                                                                                                                           16-Jan-12 9:58 PM
Send, pass, get variables with PHP, Form, HTML & JavaScript code        http://www.skytopia.com/project/articles/compsci/form.html


         Maintain a session by sending PHP variables (including multiple full blown
         multi-dimensional arrays if you so wish) to a page with PHP in.
         INSIDE "page1.php"
         <?php
                  // "session_register()" and "session_start();" both prepare the session ready for use, and "$myPHPvar=5"
                  // is the variable we want to carry over to the new page. Just before we visit the new page, we need to
                  // store the variable in PHP's special session area by using "$_SESSION['myPHPvar'] = $myPHPvar;"
                  session_register();
                  session_start();
                  $myPHPvar=5;
                  $_SESSION['myPHPvar'] = $myPHPvar;
         ?>

         <a href="page2.php">Click this link</a>, and the "$myPHPvar" variable should carry through.

         INSIDE "page2.php"
         <?php
                  // Retrieve the PHP variable (using PHP).
                  session_start();
                  $myPHPvar = $_SESSION['myPHPvar'];
                  echo "myPHPvar: ".$myPHPvar." ..........(should say "myPHPvar: 5")<br>";
         ?>




         Allow sending hidden form variables to a PHP page. This allows the user to
         select options on the page, and for these to be carried across to the new page.
         EXAMPLE:

         (these links all go to the same PHP page...)
         Click 1st
         Click 2nd
         Click 3rd


              Click 1st
              Click 2nd
              Click 3rd




         INSIDE "page1.php" or "page1.html"

         <!-- Pass over to the new page an arbitrary value, which in this case, is determined by... -->


         <!-- ***********    the link being clicked: (USE THIS OR...) ************** -->

         <a href="#" onclick="document.myform.formVar.value='first'; document.myform.submit(); return false">Click 1st</a><br>
         <a href="#" onclick="document.myform.formVar.value='second'; document.myform.submit(); return false">Click 2nd</a><br>
         <a href="#" onclick="document.myform.formVar.value='third'; document.myform.submit(); return false">Click 3rd</a><br>

         <!-- ***********    the radio button pressed before clicking "Send Form" (...OR USE THIS OR...) ************** -->

         <input name="br" type="radio" onClick="document.myform.formVar.value='first'">Click 1st<br>
         <input name="br" type="radio" onClick="document.myform.formVar.value='second'">Click 2nd<br>
         <input name="br" type="radio" onClick="document.myform.formVar.value='third'">Click 3rd<br><br>

         <!-- *********** the dropdown menu selected before clicking "Send Form" (...OR USE THIS) ************** -->

         <select onchange="document.myform.formVar.value=this.value">
                 <option value="first">Select 1st</option>
                 <option value="second">Select 2nd</option>
                 <option value="third">Select 3rd</option>




3 of 6                                                                                                            16-Jan-12 9:58 PM
Send, pass, get variables with PHP, Form, HTML & JavaScript code      http://www.skytopia.com/project/articles/compsci/form.html


         </select>

         <!-- ***************************************** -->


         <!-- In each of the above three cases, the hidden variable in the code below is needed for it all to work.
         Also notice how the destination page is given here, rather than in anything above -->

         <form method=post name="myform" action="page2.php">
                 <input type="hidden" name="formVar" value="">
                 <input type="submit" value="Send form!">
         </form>



         INSIDE "page2.php"
         <?php
                  // Retrieve the hidden form variable (using PHP).
                  $myvar = $_POST['formVar'];
                  echo "myvar: ".$myvar;
         ?>




         Allow sending hidden form variables (using tick boxes) to a PHP page. This
         allows the user to select options on the page, and for these to be carried
         across to the new page.
         EXAMPLE:

         INSIDE "page1.html"

         Tick A
         Tick B
         Tick C




         CODE:

         INSIDE "page1.html"
         Tick A <input type="checkbox" checked onClick="document.myform2.a0.value = this.checked"><br>
         Tick B <input type="checkbox" onClick="document.myform2.b0.value = this.checked"><br>
         Tick C <input type="checkbox" onClick="document.myform2.c0.value = this.checked">

         <form method=post name="myform2" action="page2b.php">
                 <input type="hidden" name="a0" value="true">
                 <input type="hidden" name="b0" value="false">
                 <input type="hidden" name="c0" value="false">
                 <input type="submit" value="Send form!">
         </form>

         INSIDE "page2b.php"
         <?php
                  // Retrieve the hidden form variable (using PHP).
                  $myvarA = $_POST['a0'];
                  $myvarB = $_POST['b0'];
                  $myvarC = $_POST['c0'];
                  echo "myvarA: ".$myvarA."<br>";
                  echo "myvarB: ".$myvarB."<br>";
                  echo "myvarC: ".$myvarC;
         ?>




4 of 6                                                                                                         16-Jan-12 9:58 PM
Send, pass, get variables with PHP, Form, HTML & JavaScript code               http://www.skytopia.com/project/articles/compsci/form.html


         Allow different bits of HTML to dynamically execute according to which radio
         button is pressed
         This is useful for many purposes including using buttons to change the picture (as part of a gallery), or for different sub forms
         to appear accordingly.

         EXAMPLE:

            Click 1st
            Click 2nd
            Click 3rd

         Anything can go here .....[I am the footer]

         CODE:
         <script type="text/javascript">
                 function SetHTML1(type) {
                         document.getElementById("a1").style.display = "none"
                         document.getElementById("b1").style.display = "none"
                         document.getElementById("c1").style.display = "none"
                         // Using style.display="block" instead of style.display="" leaves a carriage return
                         document.getElementById(type).style.display = ""
                 }
         </script>

         <input name="br" type="radio" checked onClick="SetHTML1('a1')">Click 1st<br>
         <input name="br" type="radio" onClick="SetHTML1('b1')">Click 2nd<br>
         <input name="br" type="radio" onClick="SetHTML1('c1')">Click 3rd<br><br>

         <span id="a1" style="">Anything can go here                                                          </span>
         <span id="b1" style="display:none">...like an image...<br><img src="http://www.skytopia.com/ar.png">    </span>
         <span id="c1" style="display:none">...<a href="http://www.skytopia.com">or a link</a>...                                            </span>

         .....[I am the footer] <!-- Not needed, but shows how stuff below the dynamic content makes it move up and down -->




         Allow different bits of HTML to dynamically execute according to which
         dropdown menu is selected
         EXAMPLE:

                    Anything can go here .....[I am the footer]

         CODE:
         <script type="text/javascript">
                 function SetHTML2(type) {
                   document.getElementById("a2").style.display = "none"
                   document.getElementById("b2").style.display = "none"
                   document.getElementById("c2").style.display = "none"
                   // Using style.display="block" instead of style.display="" leaves a carriage return
                   document.getElementById(type).style.display = ""
                 }
         </script>

         <select onchange="SetHTML2(this.value)">
                 <option value="a2">Select 1st</option>
                 <option value="b2">Select 2nd</option>
                 <option value="c2">Select 3rd</option>
         </select>

         <span id="a2" style="display:none">Anything can go here                                                                </span>
         <span id="b2" style="display:none">...like an image...<br><img src="http://www.skytopia.com/ar.png">                   </span>
         <span id="c2" style="display:none">...<a href="http://www.skytopia.com">or a link</a>...                               </span>

         .....[I am the footer] <!-- Not needed, but shows how stuff below the dynamic content makes it move up and down -->




5 of 6                                                                                                                          16-Jan-12 9:58 PM
Send, pass, get variables with PHP, Form, HTML & JavaScript code                                  http://www.skytopia.com/project/articles/compsci/form.html




         Add/remove bits of HTML according to multiple checkboxes
         EXAMPLE:                Anything can go here

         CODE:
         <script type="text/javascript">
                 function SetHTML3(check,type) {
                         if(check==true) document.getElementById(type).style.display = "";
                         else            document.getElementById(type).style.display = "none";
                 }
         </script>

         <input type="checkbox" CHECKED onClick="SetHTML3(this.checked,'a3')">
         <input type="checkbox" onClick="SetHTML3(this.checked,'b3')">
         <input type="checkbox" onClick="SetHTML3(this.checked,'c3')">

         <span id='a3'> <b> Anything can go here </b></span>
         <span id='b3' style="display:none"> <b> ...like an image... <img src="http://www.skytopia.com/ar.png"> </b></span>
         <span id="c3" style="display:none"> <b> ...<a href="http://www.skytopia.com">or a link</a> </b></span>




         Test your PHP pages offline easily, without installing your own web server.
         This was something recently that doubled my productivity in a snap. Simply use EasyPHP. It's only 8 megabyte, and a million
         times quicker and simpler to install/configure than a fully blown Apache (or equivalent) server. In fact, it's small GUI window
         will make it instant - just make sure you put PHP pages inside its special www folder, and point your browser to the
         "http://127.0.0.1/mywebpage.php".

         Apart from this amazing time saver, always "View source" to see what PHP outputs to the HTML/Javascript page (remember,
         PHP is read by the server, but is not potentially visible to the visitor in the same way that Javascript or HTML is).

         The last amazing tip is to insert "exit();" when an annoying bug creeps into the code to see where the code goes wrong (try
         "exit()" at different points throughout the code).



         Feedback is welcome, whether it's a chat, improvement I can make, or just a quick thanks.



                          If the info on this site has been of sufficient interest, a small donation would be appreciated:




                                                   All pictures and text on this page are copyright 2007 onwards Daniel White.
                                                 If you wish to use any images from this page, please contact me for permission.




6 of 6                                                                                                                                   16-Jan-12 9:58 PM

More Related Content

What's hot

2012.sandiego.wordcamp
2012.sandiego.wordcamp2012.sandiego.wordcamp
2012.sandiego.wordcamp
Brandon Dove
 
Php mysql
Php mysqlPhp mysql
Php mysql
Abu Bakar
 
Java script
Java scriptJava script
Java script
Fajar Baskoro
 
Lecture3 php by okello erick
Lecture3 php by okello erickLecture3 php by okello erick
Lecture3 php by okello erick
okelloerick
 
Php tutorial
Php  tutorialPhp  tutorial
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
Nikhil Jain
 
Seaside - The Revenge of Smalltalk
Seaside - The Revenge of SmalltalkSeaside - The Revenge of Smalltalk
Seaside - The Revenge of Smalltalk
Lukas Renggli
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
Brainware Consultancy Pvt Ltd
 
Session 3 Java Script
Session 3 Java ScriptSession 3 Java Script
Session 3 Java Script
Muhammad Hesham
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
team11vgnt
 
Client side scripting
Client side scriptingClient side scripting
Client side scripting
Eleonora Ciceri
 
Jsf lab
Jsf labJsf lab
Jsf lab
Yu-Ting Chen
 
Fb request form guide
Fb request form guideFb request form guide
Fb request form guide
tamirc
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
Aminiel Michael
 
Lecture8
Lecture8Lecture8
Lecture8
Majid Taghiloo
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
Ahmed Swilam
 

What's hot (18)

2012.sandiego.wordcamp
2012.sandiego.wordcamp2012.sandiego.wordcamp
2012.sandiego.wordcamp
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Java script
Java scriptJava script
Java script
 
Lecture3 php by okello erick
Lecture3 php by okello erickLecture3 php by okello erick
Lecture3 php by okello erick
 
Php tutorial
Php  tutorialPhp  tutorial
Php tutorial
 
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
 
Seaside - The Revenge of Smalltalk
Seaside - The Revenge of SmalltalkSeaside - The Revenge of Smalltalk
Seaside - The Revenge of Smalltalk
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
Session 3 Java Script
Session 3 Java ScriptSession 3 Java Script
Session 3 Java Script
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
 
Client side scripting
Client side scriptingClient side scripting
Client side scripting
 
Jsf lab
Jsf labJsf lab
Jsf lab
 
Fb request form guide
Fb request form guideFb request form guide
Fb request form guide
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
 
Lecture8
Lecture8Lecture8
Lecture8
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 

Similar to Send, pass, get variables with php, form, html & java script code

Introduction to php
Introduction to phpIntroduction to php
Introduction to php
jgarifuna
 
PHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web DevelopmentPHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web Development
Edureka!
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
Yoeung Vibol
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
Ahmed Saihood
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
Arti Parab Academics
 
php.pdf
php.pdfphp.pdf
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.ppt
ElieNGOMSEU
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.ppt
SibabrataChoudhury2
 
Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....
Lalith86
 
PHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connecting
PraveenHegde20
 
forms.pptx
forms.pptxforms.pptx
forms.pptx
asmabagersh
 
Learning of Php and My SQL Tutorial | For Beginners
Learning of Php and My SQL Tutorial | For BeginnersLearning of Php and My SQL Tutorial | For Beginners
Learning of Php and My SQL Tutorial | For Beginners
Ratnesh Pandey
 
Php My SQL Tutorial | beginning
Php My SQL Tutorial | beginningPhp My SQL Tutorial | beginning
Web server scripting - Using a form
Web server scripting - Using a formWeb server scripting - Using a form
Web server scripting - Using a form
John Robinson
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
Gheyath M. Othman
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
husnara mohammad
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
HambardeAtharva
 
php 1
php 1php 1
php 1
tumetr1
 
From CakePHP to Laravel
From CakePHP to LaravelFrom CakePHP to Laravel
From CakePHP to Laravel
Jason McCreary
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
Taha Malampatti
 

Similar to Send, pass, get variables with php, form, html & java script code (20)

Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web DevelopmentPHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web Development
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 
php.pdf
php.pdfphp.pdf
php.pdf
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.ppt
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.ppt
 
Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....
 
PHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connecting
 
forms.pptx
forms.pptxforms.pptx
forms.pptx
 
Learning of Php and My SQL Tutorial | For Beginners
Learning of Php and My SQL Tutorial | For BeginnersLearning of Php and My SQL Tutorial | For Beginners
Learning of Php and My SQL Tutorial | For Beginners
 
Php My SQL Tutorial | beginning
Php My SQL Tutorial | beginningPhp My SQL Tutorial | beginning
Php My SQL Tutorial | beginning
 
Web server scripting - Using a form
Web server scripting - Using a formWeb server scripting - Using a form
Web server scripting - Using a form
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
 
php 1
php 1php 1
php 1
 
From CakePHP to Laravel
From CakePHP to LaravelFrom CakePHP to Laravel
From CakePHP to Laravel
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 

Recently uploaded

Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Pitangent Analytics & Technology Solutions Pvt. Ltd
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
LizaNolte
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 

Recently uploaded (20)

Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 

Send, pass, get variables with php, form, html & java script code

  • 1. Send, pass, get variables with PHP, Form, HTML & JavaScript code http://www.skytopia.com/project/articles/compsci/form.html Skytopia > Projects > Technology/science/misc articles > HTML Forms in a nutshell Ads by Google PHP CMS Script PHP Form Post PHP Super useful bits of PHP, Form and JavaScript code All code on this page is considered by us to contain the most basic and important aspects of form SpeedXML sending. This page basically shows in the shortest possible way how you can: Allows to extract import XML Test your PHP offline - Test your PHP pages offline easily, without installing your Data Free evaluation copy own web server. on line www.idellys.com/soluti Send variables between Javascript, Form and PHP on the same page: * PHP variable to Javascript variable or send Form variable to Javascript variable * PHP variable to Form variable or send Javascript variable to Form variable * Javascript variable to PHP variable (impossible) or send Form variable to PHP variable (impossible) Send variables from one page to another: * URL variables to PHP/JavaScript page - Allow sending different variables via the URL string to the new page (readable through PHP or Javascript). This is more appropriate than the below if there are few pieces of small info you wish to send. * PHP variables to PHP page - Alternatively, maintain a session by sending PHP variables to another page with PHP in (allows super large arrays). * Form variables to PHP page - Alternatively, allow sending hidden form variables to a PHP page. This allows the user to (more easily than the above methods) select options on the page (via URL, radio buttons, or dropdown menu), and for these to be sent to the new page. * Form variables (tick boxes) to PHP page - As above but with tick boxes. Dynamically update HTML - Allow different bits of HTML to dynamically execute according to user input: * Change HTML according to radio button selection. * Change HTML according to dropbox selection. * Add/remove bits of HTML according to multiple checkboxes. Send variables between Javascript, Form and PHP on the same page: PHP variable to Javascript variable: <?php $myvar=10; ?> <script type="text/javascript"> jsvar = <?php echo $myvar; ?>; document.write(jsvar); // Test to see if its prints 10: </script> 1 of 6 16-Jan-12 9:58 PM
  • 2. Send, pass, get variables with PHP, Form, HTML & JavaScript code http://www.skytopia.com/project/articles/compsci/form.html Form variable to Javascript variable: <form name="myform4"> <input type="hidden" name="formvar" value="100"> </form> <script type="text/javascript"> jsvar = document.myform4.formvar.value; document.write(jsvar) // test </script> PHP variable to Form variable: <form name="myform4"> <input type="hidden" name="formvar" value="<?php $phpvar=10; echo $phpvar; ?>"> // PHP code inside HTML!! </form> Javascript variable to Form variable: <form name="myform3"> <!-- It needn't be a "hidden" type, but anything from radio buttons to check boxes --> <input type="hidden" name="formvar" value=""> </form> <script type="text/javascript"> jsvar=10; document.myform3.formvar.value = jsvar; </script> Javascript variable to PHP variable: This is impossible (unless you reload the page, or call another page), since all PHP code is rendered first (on the server side), and then Javascript is dealt with afterwards (on the client side). Form variable to PHP variable: Without refreshing, this is impossible like above for similar reasons. If you don't mind a refresh, then either a page reload or calling another web page would work if you used something like $_POST['FormVar']; in the php file. Allow sending different variables via the URL string to the new page (readable through PHP or Javascript). The version shown below uses PHP to read the variables back, but it's possible to use Javascript and some messy splitting to do the same (for that route, see this nice little function, or here, or here for example code.) EXAMPLE: Send variables via URL! INSIDE "page1.php" or "page1.html" // Send the variables myNumber=1 and myFruit="orange" to the new PHP page... <a href="page2c.php?myNumber=1&myFruit=orange">Send variables via URL!</a> INSIDE "page2c.php" <?php // Retrieve the URL variables (using PHP). $num = $_GET['myNumber']; $fruit = $_GET['myFruit']; echo "Number: ".$num." Fruit: ".$fruit; ?> 2 of 6 16-Jan-12 9:58 PM
  • 3. Send, pass, get variables with PHP, Form, HTML & JavaScript code http://www.skytopia.com/project/articles/compsci/form.html Maintain a session by sending PHP variables (including multiple full blown multi-dimensional arrays if you so wish) to a page with PHP in. INSIDE "page1.php" <?php // "session_register()" and "session_start();" both prepare the session ready for use, and "$myPHPvar=5" // is the variable we want to carry over to the new page. Just before we visit the new page, we need to // store the variable in PHP's special session area by using "$_SESSION['myPHPvar'] = $myPHPvar;" session_register(); session_start(); $myPHPvar=5; $_SESSION['myPHPvar'] = $myPHPvar; ?> <a href="page2.php">Click this link</a>, and the "$myPHPvar" variable should carry through. INSIDE "page2.php" <?php // Retrieve the PHP variable (using PHP). session_start(); $myPHPvar = $_SESSION['myPHPvar']; echo "myPHPvar: ".$myPHPvar." ..........(should say "myPHPvar: 5")<br>"; ?> Allow sending hidden form variables to a PHP page. This allows the user to select options on the page, and for these to be carried across to the new page. EXAMPLE: (these links all go to the same PHP page...) Click 1st Click 2nd Click 3rd Click 1st Click 2nd Click 3rd INSIDE "page1.php" or "page1.html" <!-- Pass over to the new page an arbitrary value, which in this case, is determined by... --> <!-- *********** the link being clicked: (USE THIS OR...) ************** --> <a href="#" onclick="document.myform.formVar.value='first'; document.myform.submit(); return false">Click 1st</a><br> <a href="#" onclick="document.myform.formVar.value='second'; document.myform.submit(); return false">Click 2nd</a><br> <a href="#" onclick="document.myform.formVar.value='third'; document.myform.submit(); return false">Click 3rd</a><br> <!-- *********** the radio button pressed before clicking "Send Form" (...OR USE THIS OR...) ************** --> <input name="br" type="radio" onClick="document.myform.formVar.value='first'">Click 1st<br> <input name="br" type="radio" onClick="document.myform.formVar.value='second'">Click 2nd<br> <input name="br" type="radio" onClick="document.myform.formVar.value='third'">Click 3rd<br><br> <!-- *********** the dropdown menu selected before clicking "Send Form" (...OR USE THIS) ************** --> <select onchange="document.myform.formVar.value=this.value"> <option value="first">Select 1st</option> <option value="second">Select 2nd</option> <option value="third">Select 3rd</option> 3 of 6 16-Jan-12 9:58 PM
  • 4. Send, pass, get variables with PHP, Form, HTML & JavaScript code http://www.skytopia.com/project/articles/compsci/form.html </select> <!-- ***************************************** --> <!-- In each of the above three cases, the hidden variable in the code below is needed for it all to work. Also notice how the destination page is given here, rather than in anything above --> <form method=post name="myform" action="page2.php"> <input type="hidden" name="formVar" value=""> <input type="submit" value="Send form!"> </form> INSIDE "page2.php" <?php // Retrieve the hidden form variable (using PHP). $myvar = $_POST['formVar']; echo "myvar: ".$myvar; ?> Allow sending hidden form variables (using tick boxes) to a PHP page. This allows the user to select options on the page, and for these to be carried across to the new page. EXAMPLE: INSIDE "page1.html" Tick A Tick B Tick C CODE: INSIDE "page1.html" Tick A <input type="checkbox" checked onClick="document.myform2.a0.value = this.checked"><br> Tick B <input type="checkbox" onClick="document.myform2.b0.value = this.checked"><br> Tick C <input type="checkbox" onClick="document.myform2.c0.value = this.checked"> <form method=post name="myform2" action="page2b.php"> <input type="hidden" name="a0" value="true"> <input type="hidden" name="b0" value="false"> <input type="hidden" name="c0" value="false"> <input type="submit" value="Send form!"> </form> INSIDE "page2b.php" <?php // Retrieve the hidden form variable (using PHP). $myvarA = $_POST['a0']; $myvarB = $_POST['b0']; $myvarC = $_POST['c0']; echo "myvarA: ".$myvarA."<br>"; echo "myvarB: ".$myvarB."<br>"; echo "myvarC: ".$myvarC; ?> 4 of 6 16-Jan-12 9:58 PM
  • 5. Send, pass, get variables with PHP, Form, HTML & JavaScript code http://www.skytopia.com/project/articles/compsci/form.html Allow different bits of HTML to dynamically execute according to which radio button is pressed This is useful for many purposes including using buttons to change the picture (as part of a gallery), or for different sub forms to appear accordingly. EXAMPLE: Click 1st Click 2nd Click 3rd Anything can go here .....[I am the footer] CODE: <script type="text/javascript"> function SetHTML1(type) { document.getElementById("a1").style.display = "none" document.getElementById("b1").style.display = "none" document.getElementById("c1").style.display = "none" // Using style.display="block" instead of style.display="" leaves a carriage return document.getElementById(type).style.display = "" } </script> <input name="br" type="radio" checked onClick="SetHTML1('a1')">Click 1st<br> <input name="br" type="radio" onClick="SetHTML1('b1')">Click 2nd<br> <input name="br" type="radio" onClick="SetHTML1('c1')">Click 3rd<br><br> <span id="a1" style="">Anything can go here </span> <span id="b1" style="display:none">...like an image...<br><img src="http://www.skytopia.com/ar.png"> </span> <span id="c1" style="display:none">...<a href="http://www.skytopia.com">or a link</a>... </span> .....[I am the footer] <!-- Not needed, but shows how stuff below the dynamic content makes it move up and down --> Allow different bits of HTML to dynamically execute according to which dropdown menu is selected EXAMPLE: Anything can go here .....[I am the footer] CODE: <script type="text/javascript"> function SetHTML2(type) { document.getElementById("a2").style.display = "none" document.getElementById("b2").style.display = "none" document.getElementById("c2").style.display = "none" // Using style.display="block" instead of style.display="" leaves a carriage return document.getElementById(type).style.display = "" } </script> <select onchange="SetHTML2(this.value)"> <option value="a2">Select 1st</option> <option value="b2">Select 2nd</option> <option value="c2">Select 3rd</option> </select> <span id="a2" style="display:none">Anything can go here </span> <span id="b2" style="display:none">...like an image...<br><img src="http://www.skytopia.com/ar.png"> </span> <span id="c2" style="display:none">...<a href="http://www.skytopia.com">or a link</a>... </span> .....[I am the footer] <!-- Not needed, but shows how stuff below the dynamic content makes it move up and down --> 5 of 6 16-Jan-12 9:58 PM
  • 6. Send, pass, get variables with PHP, Form, HTML & JavaScript code http://www.skytopia.com/project/articles/compsci/form.html Add/remove bits of HTML according to multiple checkboxes EXAMPLE: Anything can go here CODE: <script type="text/javascript"> function SetHTML3(check,type) { if(check==true) document.getElementById(type).style.display = ""; else document.getElementById(type).style.display = "none"; } </script> <input type="checkbox" CHECKED onClick="SetHTML3(this.checked,'a3')"> <input type="checkbox" onClick="SetHTML3(this.checked,'b3')"> <input type="checkbox" onClick="SetHTML3(this.checked,'c3')"> <span id='a3'> <b> Anything can go here </b></span> <span id='b3' style="display:none"> <b> ...like an image... <img src="http://www.skytopia.com/ar.png"> </b></span> <span id="c3" style="display:none"> <b> ...<a href="http://www.skytopia.com">or a link</a> </b></span> Test your PHP pages offline easily, without installing your own web server. This was something recently that doubled my productivity in a snap. Simply use EasyPHP. It's only 8 megabyte, and a million times quicker and simpler to install/configure than a fully blown Apache (or equivalent) server. In fact, it's small GUI window will make it instant - just make sure you put PHP pages inside its special www folder, and point your browser to the "http://127.0.0.1/mywebpage.php". Apart from this amazing time saver, always "View source" to see what PHP outputs to the HTML/Javascript page (remember, PHP is read by the server, but is not potentially visible to the visitor in the same way that Javascript or HTML is). The last amazing tip is to insert "exit();" when an annoying bug creeps into the code to see where the code goes wrong (try "exit()" at different points throughout the code). Feedback is welcome, whether it's a chat, improvement I can make, or just a quick thanks. If the info on this site has been of sufficient interest, a small donation would be appreciated: All pictures and text on this page are copyright 2007 onwards Daniel White. If you wish to use any images from this page, please contact me for permission. 6 of 6 16-Jan-12 9:58 PM