Reputation: 1904
The user enters a number and clicks submit. The number now shows up on the page.
The user is then asked if they would like to double the number. They click yes.
The doubled number now appears on the page.
I am having trouble with part 3. Is this possible using just PHP?
UPDATE: Thanks for your answers. This is my first ever PHP script, so I wasn't sure. I am going to research doing it with AJAX just now. I'm very curious to know why it is possible to get to part 2 if you can't get to part 3. Can anyone explain this or provide a link?
Upvotes: 0
Views: 4295
Reputation: 2556
This is really more of a comment than an answer, but SO limits comment length so here 'tis:
The misconception that a web page is synonymous with a PHP script appears to be common among novice PHP programmers. Really, a web page is an HTML document (with associated resources) that is sent from a web server to a web browser as part of an HTTP response. A single server-side script can generate many different kinds of web pages, and when processing HTML forms (as you seem to be doing) it is common for a single server-side script to do exactly that. In "web 1.0" applications, clicking on "Submit" (for example) typically causes the browser to make a new HTTP request (called a "page turn"), and the web server keeps track of what the user is doing across page turns by storing "state" either in a "session" (with an associated key included in the HTTP header of each HTTP request - and the HTTP response generated by the web "login") or in one or more HTTP parameters. The point is that each new HTTP request may be for the same script/URL on the server, but the behavior (and the appearance of the web page) will be different because the server is somehow tracking the state of the "workflow" across page turns.
Now if you really must avoid page turns, you can use Javascript to change the appearance/state of the page displayed by the browser without making the user click on a "Submit" button. And the XMLHTTPRequest mechanism (aka AJAX), which allows content to be fetched from the web server "behind the scenes" and change the state of the client-side document without the user doing anything, is typically used to achieve this. But is only really necessary if you're doing something rather different (e.g. scrolling a map or updating a stock ticker) than what you describe in your question, which looks like a perfect example of a "web 1.0" application workflow.
Upvotes: 0
Reputation: 16091
<?php
$double = (isset($_REQUEST['do_double']) && $_REQUEST['do_double'] == '1') ? ($_REQUEST['number'] * 2) : '';
?>
<form method="get" action="?">
<input type="hidden" name="do_double" value="<?php echo isset($_REQUEST['number']) ?'1' : '0'; ?>" />
<input type="text" name="number" value="<?php echo isset($_REQUEST['number']) ? $_REQUEST['number'] : '';?>" />
<?php if ( ! isset($_REQUEST['number'])) { ?>
<input type="submit" value="submit" />
<?php } else { ?>
<input type="submit" value="Verdoppeln" />
<?php } ?>
</form>
<div id="number"><?php echo $double; ?></div>
Upvotes: 1
Reputation: 31077
I think you're talking about session variables. At the top of the script add the following script to start a session for the current user:
session_start();
This will allow you to store variables in session $_SESSION
which persists between requests. Use isset
to check if a value is set in session.
Upvotes: 1
Reputation: 189
Asynchronous is required I think. Try these 2 reads:
the difference between synchronous and async: http://javascript.about.com/od/ajax/a/ajaxasyn.htm and
possible examples: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
Upvotes: 0
Reputation: 13975
Take the form ID using something like document.formname.inputname
(http://www.quirksmode.org/js/forms.html) and then multiple by two then use javascript to show it where ever you want.
Upvotes: 0