Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › [PHP] Page redirect and form post problem
New Posts  All Forums:Forum Nav:

[PHP] Page redirect and form post problem

post #1 of 9
Thread Starter 
When I submit a validated page through this form http://glenwoodpharmacy.com/bim.php, It redirects to a blank page(itself) and then it goes to the page that would send the email. When it posts to itself, the form is cleared and probably all the variables with it, and then the Captcha, which relies on sessions fails to validate...

Also If you know of a better way to do it / tutorial, point me in the direction, this is kind of a mash up of various sources/methods.

When you submit the validated form, it displays the same page, but empty, then redirects to the page that sends the email (if the captcha verifies) I think theirs an error in the logic of the page, right now its

submit validated data > shows same page but empty > then redirects to email

should be:

submit validated data > goes directly to email page to be verified by captcha

code below:

Code:

//print a text box
function input_text($element_name, $values, $size) {
    print '<input type="text" name="' . $element_name .'" value="';
    print htmlentities($values[$element_name]) . '"';
   print ' style="width: ' . $size . '">';
}
//print a text area
function input_textarea($element_name, $values) {
    print '<textarea name="' . $element_name .'"';
   print ' style="width: 248px; height: 90px;">';
    print htmlentities($values[$element_name]) . '</textarea>';
}

// The main page logic:
// - If the form is submitted, validate and then process or redisplay
// - If it's not submitted, display
if ($_POST['_submit_check']) {
    // If validate_form() returns errors, pass them to show_form()
    if ($form_errors = validate_form()) {
        show_form($form_errors);
    } else {
        // The submitted data is valid, so process it
        process_form();
    }
} else {
    // The form wasn't submitted, so display
    show_form();
}

function show_form($errors = '') {
   // If the form is submitted, get defaults from submitted parameters
    if ($_POST['_submit_check']) 
    {
       $defaults = $_POST;
    }

    if ($errors) {
        $error_text = '<p>You need to correct the following errors:';
        $error_text .= '<ul><li>';
        $error_text .= implode('</li><li>',$errors);
        $error_text .= '</li></ul></p>';
    } else {
        $errors = '';
}
?>
<h1>Prescription Refill Page</h1>

<form method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>">
<table style="width: 60%; margin-left: 2em;">
   <tr>
      <td>Name</td><td style="width: 80%;"><?php input_text('name', $defaults, '187px') ?>
        </td>
   </tr>
   <tr>
      <td>Email</td><td><?php input_text('email', $defaults, '187px') ?>
        </td>
   </tr>
   <tr>
      <td>Phone</td><td><?php input_text('phone', $defaults, '80px') ?></td>
   </tr>
   <tr>
      <td>RX</td><td><?php input_text('rx', $defaults, '139px') ?></td>
   </tr>
   <tr>
      <td colspan="2" style="text-align: center;"><br /></td>
   </tr>
   <tr>
      <td colspan="2" style="text-align:center;"><?php input_textarea('comments', $defaults); ?>
        </td>
   </tr>
   <tr>
      <td colspan="2" style="text-align: center;">
        <p style="text-align: center;" align="center">
<img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" /><br />
<br />
<input type="text" name="captcha_code" size="10" maxlength="6" /> <a href="" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">Reload Image</a></p>

        <input name="send" type="submit" id="send" value="Submit"><br />
      <div align="center" class="errmsg"><br /><?php print $error_text;?></div>    
        </td>
   </tr>
</table>
<input type="hidden" name="_submit_check" value="1"/>
</form>

<?php
}

function validate_form() {
    $errors = array();

function isEmail($email)
  {
   return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]]).)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]).){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
  }
   
   if (! strlen(trim($_POST['name']))) {
        $errors[] = 'Please enter your name.';
    }
   if (! strlen(trim($_POST['email']))) {
        $errors[] = 'Please enter your Email address';
    }
   if(!isEmail(trim($_POST['email']))) {
      $errors[] = 'Your Email address is not valid';
   }
   if (! strlen(trim($_POST['phone']))) {
        $errors[] = 'Please enter Phone number';
    }
   if (! strlen(trim($_POST['rx']))) {
        $errors[] = 'Please enter your RX Number';
    }
   if (! strlen(trim($_POST['comments']))) {
        $errors[] = 'Please enter your Comments';
    }
   
    return $errors;
}

function process_form() 
{
  print "<meta http-equiv=\\"refresh" content=\\"0;URL=refill.php">";
}

Edited by Faster_is_better - 12/17/08 at 12:05pm
post #2 of 9
i tried testing it out but it just kept saying that the captcha code is incorrect.
 
Home Server
(6 items)
 
 
CPUMotherboardRAMHard Drive
intel 4770k asus gryphon corsair vengeance samsung evo 
CoolingOSPowerCase
corsair h100 windows 7 ultimate rm650 caselabs s5 
CPUMotherboardRAMHard Drive
intel 2100 asrock  gskill 4gb crucial m4 64gb 
OSCase
windows server 2012 r2 xion 
  hide details  
Reply
 
Home Server
(6 items)
 
 
CPUMotherboardRAMHard Drive
intel 4770k asus gryphon corsair vengeance samsung evo 
CoolingOSPowerCase
corsair h100 windows 7 ultimate rm650 caselabs s5 
CPUMotherboardRAMHard Drive
intel 2100 asrock  gskill 4gb crucial m4 64gb 
OSCase
windows server 2012 r2 xion 
  hide details  
Reply
post #3 of 9
Thread Starter 
Right, you see how the page sends(when everything is validated) and it goes blank, THEN redirects, need to cut out that part where it loses the form and all the variables..
post #4 of 9
i know whats wrong. bottom of page:

function process_form()
{
print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=refill.php\\">";
}


why not just go straight to refill.php instead of refreshing the whole page. try this instead


function process_form()
{
header('Location: http://glenwoodpharmacy.com/refill.php');
}
 
Home Server
(6 items)
 
 
CPUMotherboardRAMHard Drive
intel 4770k asus gryphon corsair vengeance samsung evo 
CoolingOSPowerCase
corsair h100 windows 7 ultimate rm650 caselabs s5 
CPUMotherboardRAMHard Drive
intel 2100 asrock  gskill 4gb crucial m4 64gb 
OSCase
windows server 2012 r2 xion 
  hide details  
Reply
 
Home Server
(6 items)
 
 
CPUMotherboardRAMHard Drive
intel 4770k asus gryphon corsair vengeance samsung evo 
CoolingOSPowerCase
corsair h100 windows 7 ultimate rm650 caselabs s5 
CPUMotherboardRAMHard Drive
intel 2100 asrock  gskill 4gb crucial m4 64gb 
OSCase
windows server 2012 r2 xion 
  hide details  
Reply
post #5 of 9
Thread Starter 
Quote:
Originally Posted by koulaid View Post
i know whats wrong. bottom of page:

function process_form()
{
print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=refill.php\\">";
}


why not just go straight to refill.php instead of refreshing the whole page. try this instead


function process_form()
{
header('Location: http://glenwoodpharmacy.com/refill.php');
}
hmmm, I thought of that, but you can't send like that, I put that bit of code at the top of the page, and it errors out

Warning: Cannot modify header information - headers already sent by (output started at /home/glenphrm/public_html/bim.php:18)
post #6 of 9
Why are you using print? Echo is a lot more versatile.

Any why create functions to print out the input fields? What advantage does that give you?
AMD Power
(16 items)
 
Macbook Pro
(13 items)
 
Web Server
(13 items)
 
CPUMotherboardGraphicsRAM
AMD FX 9590 Asrock 990FX Extreme9 ASUS Radeon HD 7870 2GB 8GB (2x4GB) G.SKILL 1866 
Hard DriveOptical DriveCoolingOS
Hitachi 1TB Blu-Ray reader / DVD burner Coolermaster Water Cooling kit bundled with 9590 Windows 10 x64 
MonitorMonitorMonitorKeyboard
Dell 2209WA Dell 2209WA Dell 2209WA Logitech G910 Orion Spark 
PowerCaseMouseMouse Pad
SeaSonic S12G 650W Corsair Vengeance C70 Gunmetal Black Razer DeathAdder 2013 Ripper XXL 
CPUMotherboardGraphicsRAM
Intel i7  Apple Intel HD 3000 / AMD Radeon HD 6750M 16GB (2x4GB) GEIL DDR3-1333 
Hard DriveOptical DriveOSMonitor
SAMSUNG 840 EVO Apple Combo Drive OS X 10.9 Mavericks 15" 1680x1050 
PowerCase
Apple power brick Aluminum 
CPUMotherboardGraphicsRAM
Phenom II 940 BE @3.2GHz DFI LanParty 7200GS 8GB DDR2 GSkill 1000 
Hard DriveOSMonitorPower
30 GB SSD (webroot), 80GB (OS) Gentoo amd64 SSH 430 Watt Rosewill 
  hide details  
Reply
AMD Power
(16 items)
 
Macbook Pro
(13 items)
 
Web Server
(13 items)
 
CPUMotherboardGraphicsRAM
AMD FX 9590 Asrock 990FX Extreme9 ASUS Radeon HD 7870 2GB 8GB (2x4GB) G.SKILL 1866 
Hard DriveOptical DriveCoolingOS
Hitachi 1TB Blu-Ray reader / DVD burner Coolermaster Water Cooling kit bundled with 9590 Windows 10 x64 
MonitorMonitorMonitorKeyboard
Dell 2209WA Dell 2209WA Dell 2209WA Logitech G910 Orion Spark 
PowerCaseMouseMouse Pad
SeaSonic S12G 650W Corsair Vengeance C70 Gunmetal Black Razer DeathAdder 2013 Ripper XXL 
CPUMotherboardGraphicsRAM
Intel i7  Apple Intel HD 3000 / AMD Radeon HD 6750M 16GB (2x4GB) GEIL DDR3-1333 
Hard DriveOptical DriveOSMonitor
SAMSUNG 840 EVO Apple Combo Drive OS X 10.9 Mavericks 15" 1680x1050 
PowerCase
Apple power brick Aluminum 
CPUMotherboardGraphicsRAM
Phenom II 940 BE @3.2GHz DFI LanParty 7200GS 8GB DDR2 GSkill 1000 
Hard DriveOSMonitorPower
30 GB SSD (webroot), 80GB (OS) Gentoo amd64 SSH 430 Watt Rosewill 
  hide details  
Reply
post #7 of 9
turn output buffering on. put this at the very top of your page:

ob_start();

and this at the end of the page

ob_end_flush();
 
Home Server
(6 items)
 
 
CPUMotherboardRAMHard Drive
intel 4770k asus gryphon corsair vengeance samsung evo 
CoolingOSPowerCase
corsair h100 windows 7 ultimate rm650 caselabs s5 
CPUMotherboardRAMHard Drive
intel 2100 asrock  gskill 4gb crucial m4 64gb 
OSCase
windows server 2012 r2 xion 
  hide details  
Reply
 
Home Server
(6 items)
 
 
CPUMotherboardRAMHard Drive
intel 4770k asus gryphon corsair vengeance samsung evo 
CoolingOSPowerCase
corsair h100 windows 7 ultimate rm650 caselabs s5 
CPUMotherboardRAMHard Drive
intel 2100 asrock  gskill 4gb crucial m4 64gb 
OSCase
windows server 2012 r2 xion 
  hide details  
Reply
post #8 of 9
Thread Starter 
Quote:
Originally Posted by timw4mail View Post
Why are you using print? Echo is a lot more versatile.

Any why create functions to print out the input fields? What advantage does that give you?
Is their much of a difference? Also I've just adapted some other code trying to make this work, may or may not be the best way, seems to go straight to the other page, but the captcha doesn't like it

Captcha is from http://www.phpcaptcha.org/documentation/quickstart/

IF you know of a tutorial or something that can give me javascript and php form validation similar to what I already have, that will work with this CAPTCHA, I would use it as this doesn't look to be the best way of doing it..
post #9 of 9
Quote:
Originally Posted by koulaid View Post
turn output buffering on. put this at the very top of your page:

ob_start();

and this at the end of the page

ob_end_flush();
This is unnecessarily, but can yield a little performance gain in some server environments like Apache, and because it's overclock.net ... probably it's a good choice.

About the echo vs print ... I don't think there is much difference, but I was used to echo, and recommend it more than print. As of now, I do not use echo or print, only die for simple output. Templates are used for the real job.
Edited by metala - 12/17/08 at 2:20pm
Ferberite
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz Lenovo Thinkpad Edge E520 AMD Radeon HD 6630M 6GB DDR3 @ 1333MHz  
Hard DriveHard DriveOSOS
Samsung 850EVO HITACHI HTS727550A9E364 7.2krpm Debian 7.0 Win7 
OSMonitorMonitorKeyboard
Win8 15.6 Zoll 16:9, 1366x768 Pixel, AUO23EC, spiege... 24" Dell U2412M, 1920x1200 Integrated + External 
PowerCase
20V, 4.5A Lenovo Thinkpad Edge 
  hide details  
Reply
Ferberite
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz Lenovo Thinkpad Edge E520 AMD Radeon HD 6630M 6GB DDR3 @ 1333MHz  
Hard DriveHard DriveOSOS
Samsung 850EVO HITACHI HTS727550A9E364 7.2krpm Debian 7.0 Win7 
OSMonitorMonitorKeyboard
Win8 15.6 Zoll 16:9, 1366x768 Pixel, AUO23EC, spiege... 24" Dell U2412M, 1920x1200 Integrated + External 
PowerCase
20V, 4.5A Lenovo Thinkpad Edge 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › [PHP] Page redirect and form post problem