javascript - Set php session value to text box value -
I am working on a PHP "interactive text game" for an assignment.
In my program, the user is prompted to enter information in various text boxes (character name, etc.), which appear after each other, because the user completes the previous text field is.
What I want to do, receives the value of the text box (user input) and stores it in the PHP session. I understand that I want to use AJAX to get the text value dynamically.
I do not want to click on the "Submit" button first, I have to accumulate the value when the user presses or clicks outside the box.
Here is a sample of code I have not yet including unnecessary lines like / with Ajax / Stylesheets etc.
The key pressing code, that is, this part of the $ ('Input') method should be obtained with the
method, text code "# name"
and ... it's somewhere PHP code should be given in? ? I am very new to PHP and Javascript: php session_start (), (
& lt $ _SESSION ['name'];> gt; & lt; top & Gt; & lt; script & gt; $ (document) .ready (function () {$ ('input'). Focus (function () {$ (this) .val (""); $ ('input ') .press (function). // focusout (function () {if (event.which === 13) {$ (this) .parent () next (' div.sub- container ') show () ; .. var xmlhttp = new XMLHttpRequest (); xmlhttp.onreadystatechange = function () {if (xmlhttp.readyState == 4 & amp; xmlhttp.status == 200} {document.getElementById ("name") innerHTML = Xmlhttp.responseText; Alert (xmlhttp.responseText);}} Xmlhttp.open ("GET", "globals.php? Q =" + $ (this) .val (), True); xmlhttp.send ();}}) $ $ ('input') Focus out (function () {$ (this) .Parent (next) ('div.sub-container'). Show (); }};}}}}; & Lt; / script> gt; body & gt; div class = "container" & gt; form action = "" Method = "go" & gt; div class = "sub-container-init" & gt; & Lt; P class = "text" & gt; Like all good stories, we need a motivational protagonist. Any person with good sound name is anything good? & Lt; / P & gt; & Lt; Input id = "name" name = "name" value = "name" /> & Lt; / Div & gt; & Lt; Input type = "submit" value = "submit" /> & Lt; / Form & gt; & Lt; / Div & gt; & Lt; / Body & gt; & Lt; / Html & gt;
This is a separate page in globals.php
. Ideally, the following PHP text will take the text from the field and store it in the session that I can get from the second page.
& lt ;? Php $ q = $ _ request ["q"]; Echo $ q; ? & Gt;
summary: How do I store a text code with a "# name"
in a PHP session, which I can access later on the same page Am I
Thank you!
on session_start (); Use the Start Page You will need to use $ _SESSION ['name']; Note that you can not echo anything before session_start or you will get an error
Comments
Post a Comment