Jump to content


  • Posts

  • Joined

  • Last visited


Profile Information

  • Gender
    Not Telling

gavinandresen's Achievements


Newbie (1/5)



  1. I wrote a function "fillInFormValues()" that makes this kind of thing easy. You put the form in a PHP string: $formHTML =<<<FORM_END <form....> ... all the form stuff... </form> FORM_END; And then you call: $filledInHTML = fillInFormValues($formHTML, $_GET); ... and magic happens. Your <selects> will be selected properly (based on what's in $_GET), your checkboxes will be checked properly... Grab it from: http://www.skypaint.com/gavin/code/ (online examples are there, too).
  2. curl is your friend. Hopefully your ISP compiled PHP --with-curl; see the section in the online PHP manual for the curl functions. If your ISP didn't, and you're running on Linux, you might be able to call the command-line curl executable via fopen()...
  3. [!--quoteo(post=362390:date=Apr 6 2006, 05:42 PM:name=BoofBoof)--][div class=\'quotetop\']QUOTE(BoofBoof @ Apr 6 2006, 05:42 PM) [snapback]362390[/snapback][/div][div class=\'quotemain\'][!--quotec--] Thanks for trying, I am still getting the exact same message when I do that so I don't know what the deal is. If you have any other suggestions, I am willing to try anything at this point. Thanks again! [/quote] You need to pass search and search2 (and any other variables from $_POST that matter) through in the 're-sort-me' column headers links. So... you'd change $_POST['search'] to $_REQUEST['search'], and make the column headers: [code] <a href="?search=$search&search2=$search2&sortBy=BLAH">BLAH</a> [/code] That will pass in search and search2 via GET (which is why you'd need to switch to _REQUEST-- you'll get them either from _POST or _GET). You might think about changing the form that shows the page to use GET rather than POST, too-- in general, use GET whenever sending the form info doesn't change any data (e.g. it just displays lists of data), and use POST when it does (e.g. adding/editing something).
  4. Here's how I did it; you can try it out at: [a href=\"http://www.skypaint.com/gavin/code/rankIt.php\" target=\"_blank\"]http://www.skypaint.com/gavin/code/rankIt.php[/a] ... and download the code from there. [code] <?php ob_start(); ?> <html> <head> <title>fillInForm: lots of select boxes example</title> <style> .error { color: red; } </style> </head> <body> <p>Sixteen, sixteen-valued selects.  Validation is making sure there are no duplicates... </p> <h1>Rank Them!</h1> <ul class="error"><li>PLACEHOLDER FOR FORM ERRORS</li></ul> <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST"> <table border="0"> <?php for ($i = 1; $i <= 16; $i++) {   $name = "rank_$i"; ?> <tr> <td><label for="<?php echo $name; ?>">Item <?php echo $name; ?>:</label></td> <td><select name="<?php echo $name; ?>" id="<?php echo $name; ?>"> <?php   for ($j = 1; $j <= 16; $j++) { echo "<option value=\"$j\">$j</option>"; } ?>   </select> </td> </tr> <?php } // Phew, end of 16 select boxes. ?> <tr><td> </td> <td><input type="submit" name="submit" value="Rank'Em"></td> </tr> </table> </form> <p><hr/><a href="fillInFormValues.zip">Download PHP code</a></p> </body> </html> <?php $html = ob_get_contents(); ob_end_clean(); require_once("fillInFormValues.php"); require_once("validateForm.php"); $request = (get_magic_quotes_gpc() ? array_map('stripslashes', $_REQUEST) : $_REQUEST); for ($i = 1; $i <= 16; $i++) {   $name = "rank_$i";   $validationData[$name] = array('isRequired', 'type' => 'number'); } if (isset($request['submit'])) {   $formErrors = validateForm($request, $validationData);   $checkDuplicates = array();   for ($i = 1; $i <= 16; $i++) {     $name = "rank_$i";     $rank = $request[$name];     if (isset($checkDuplicates[$rank])) {       $formErrors[$name] = "You ranked two or more items as number $rank";     }     else if (!empty($rank)) {       $checkDuplicates[$rank] = $name;     }   }   if (count($formErrors) == 0) {     // Normally there would be code here to process the form     // and redirect to a thank you page...     // ... but for this example, we just always re-display the form.     $info = "No errors; got these values:".       nl2br(htmlspecialchars(print_r($request, 1)));     $html = preg_replace('/<body>/', "<body><p>$info</p>", $html);   } } else {   $formErrors = array(); } echo fillInFormValues($html, $request, $formErrors); ?> [/code]
  5. [!--quoteo(post=359406:date=Mar 28 2006, 02:22 PM:name=coldkill)--][div class=\'quotetop\']QUOTE(coldkill @ Mar 28 2006, 02:22 PM) [snapback]359406[/snapback][/div][div class=\'quotemain\'][!--quotec--] How do you keep values in a form which someone has entered. [/quote] Generally, when the form is first submitted you save the values somewhere (a database is a good place). And when the user goes back, you fetch the values from the database and then display the form with values in the right spots. Is that what you mean? I have some examples of how I do something like that at [a href=\"http://www.skypaint.com/gavin/code/\" target=\"_blank\"]http://www.skypaint.com/gavin/code/[/a] ....
  6. Do you care if hackers can influence your form validation? If you do (and you probably should), then you can't put any information about what should be validate or how it should be validated in your forms (because it's really easy to change hidden fields-- e.g. grab the FireFox Web Developer extension and it lets you see and edit the hidden fields before submission). You should probably turn it around, and define what fields need to get validated in your PHP code: $needValidation = array('foo', 'bar', ... etc); foreach ($needValidation AS $field) { if (!isset($_POST[$field])) or empty($_POST[$field])) ... error.... } I handle form validation something like this: [code] $validationData['siteName'] = array('isRequired', 'type' => 'text'); $validationData['isLive'] = array('isRequired', 'type' => 'number'); $validationData['rootDirectory'] = array('isRequired', 'type' => 'text'); $validationData['defaultTemplate'] = array('isRequired', 'type' => 'text'); $validationData['siteFooter'] = array('isRequired', 'type' => 'text'); $validationData['provdist'] = array('isRequired'); if (isset($post['submit'])) {   $formErrors = validateForm($post, $validationData);   if (!preg_match('/^\w*$/', $post['rootDirectory'])) {     $formErrors['rootDirectory'] = "Invalid Location";   }   if (count($formErrors) == 0) {     // Normally there would be code here to process the form     // and redirect to a thank you page...   } } else {   $formErrors = array(); } echo fillInFormValues($html, $post, $formErrors); [/code] (try out the form at [a href=\"http://www.skypaint.com/gavin/code/longExample.php\" target=\"_blank\"]http://www.skypaint.com/gavin/code/longExample.php[/a] )
  7. [!--quoteo(post=359045:date=Mar 27 2006, 04:56 PM:name=SCook)--][div class=\'quotetop\']QUOTE(SCook @ Mar 27 2006, 04:56 PM) [snapback]359045[/snapback][/div][div class=\'quotemain\'][!--quotec--] First, is there a php function to grab this highest index, ie. what will be next, even if the table is currently empty? And second, is there a way to reset the indexes back to 0? [/quote] You using MySQL? If you are, the SQL query: [code]  SHOW TABLE STATUS tablename LIKE 'Auto_increment'; [/code] ... will return the next auto-increment value. BUT-- could there ever be two people adding images at once? If so, you might get the wrong value. Generally it's better to INSERT the record into the database and then SELECT LAST_INSERT_ID(); to get the value just inserted (you'll always get the right value there, because LAST_INSERT_ID() is connection-specific). ALTER TABLE can reset the auto-increment value to whatever you like, and TRUNCATE [i]tablename[/i] will reset it to zero.
  8. Are you submitting via method="GET" or "POST"? GET has a fairly low limit (dunno if the server or browser limit is the bottleneck... but anyways). POST usually has a couple megabyte limit (depends on how your web server and PHP are configured).
  9. First: you've gotta quote array indices, so do: if ($_POST['update']) ... $twelveam = htmlspecialchars($_POST['twelveam']); (note the ' characters inside the square brackets). Second, I'd separate the display of the form from the PHP code; I like to use the output buffering functions (ob_start()/ob_end_flush()) to grab the HTML for the form into a string, and have all the PHP logic at the end. Third, I'd take a more "data-driven" approach to save typing. I'd code it something like this: [code]<? $times = array( 'twelveam' => '12am - 1am', ... lots removed... 'elevenpm' => '11pm - 12am', ); ob_start(); ?> <center><form method="POST"> <table width=499> <?php foreach ($times AS $name => $value) { ?> <tr> <td align="right" width="25%"> <?php echo $value; ?></td><td align="left"> <input size="25" name="<?php echo $name; ?>"></td> </tr> <?php } // End of foreach $times ?> <tr> <td align="center"> </td> <td align="left"> <input type="submit" name="update" value="Update"></td> </tr> </table> </form> </center> <?php $formHTML = ob_end_flush(); // You can download fillInFormValues from: http://www.skypaint.com/gavin/code/ require_once("fillInFormValues.php"); if (!$_POST[update]) {   $schedule = mysql_query("SELECT * from schedule where day='Monday'");   $schedule = mysql_fetch_array($schedule, MYSQL_ASSOC);   echo fillInFormValues($formHTML, $schedule); } else {   // Build up UPDATE sql:   $setClause = "";   foreach ($times AS $name => $value) {     $setClause .= "$name = '".htmlspecialchars($_POST[$name])."',";   }   // Trim off trailing comma:   $setClause = rtrim($setClause, ','); echo ("Monday has been updated."); $update = mysql_query("update schedule set $setClause where day = 'Monday'"); } } else { // They aren't logged in! echo ("<a href=\"login.php\">You must login</a>"); } ?> [/code]
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.