Skip to main content
gamedev.net
🔒 Locked

[web] simple php checkboxes

Started by vanillacoke Aug 28, 2006 at 1:07 PM 2 replies 1k views
Original Post
vanillacoke
vanillacoke
I'm just kind of starting to learn php, and I put together a script that I call my "word generator." I have the whole thing working for the most part, but theres one thing that I'm having trouble getting. Basically, I have four text files full of words - each file contains prefixes or suffixes, and theres a default file of prefixes, along with a user-submitted file of prefixes, and the same goes for the suffixes. I have the user-submission working fine, and the script that generates the words works fine (refresh page to run the script again.. I think there might be a better way to do this) but I decided I wanted the user to be able to decide whether to use the two default files, or the two user-submitted files, or all four of them. I have an html form with two checkboxes on the page, and I'm trying to figure out how to use them. I need the php script (on the same page) to be able to access those checkboxes, and I need the page to be able to remember which boxes are checked when the page is refreshed. I want to know if there is any simple way to do this. I'm willing to tweak the way the script works now to get these checkboxes working. Can anyone help me out at all? thanks. the main page for the script is web.ics.purdue.edu/~abernste/words.php
You know what I never noticed before?
ToohrVyk
ToohrVyk
Once PHP has generated a page, it does not have any more knowledge of what happens to this page. However, the page may send an HTTP request back, which in turn will cause PHP to run and send back data. There are two possible ways here:

1° Form submitting: this sends the contents of the form as part of the $_GET or $_POST global variables (depending on the method used by the form). These globals contain the values that were sent by the user. You can have the form submit either through a button inside the form, or by using &#106avascript to submit the form when a link is clicked or a checkbox is altered. PHP must the generate a new page, which can take into account the returned information. <br><br>2° Asynchronous queries (such as AJAX) use &#106avascript to send a small query to the server, receive the answer and change the same page. This is harder to achieve. <br><br>Either way, refereshing a page does not send any of the client-side information (such as checked boxes) to the server, so you have to send it yourself. <br>
Sander
Sander
ThoorVyk's (1) is definately the best for something as simple as this. Basically you'd get:

// this is word.php<?php$use_file_1 = isset($_GET['use_file_1'] ? 'cheched="checked" ' : '';$use_file_2 = isset($_GET['use_file_2'] ? 'cheched="checked" ' : '';$use_file_3 = isset($_GET['use_file_3'] ? 'cheched="checked" ' : '';$use_file_4 = isset($_GET['use_file_4'] ? 'cheched="checked" ' : '';// Do stuff like....if ($use_file_1){    ....}....?><form method="get" action="word.php">    <input type="checkbox" name="use_file_1" value="yes" <?=$use_file_1?>/> Use file 1
<input type="checkbox" name="use_file_2" value="yes" <?=$use_file_2?>/> Use file 2
<input type="checkbox" name="use_file_3" value="yes" <?=$use_file_3?>/> Use file 3
<input type="checkbox" name="use_file_4" value="yes" <?=$use_file_4?>/> Use file 4
<input type="submit" name="generate" value="Generate" /></form>
vanillacoke
vanillacoke
Thanks for the replies. while I was fooling around I figured out a way to do it, I'm not sure if its basically the same thing suggested here or if I did something different. And I'm not really sure if what I did was a good way to do it, but it seems to work pretty well.
You know what I never noticed before?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.