Jump to content

could not get post values of a form submission


adrianmak
 Share

Recommended Posts

order template

<?php

$id = $input->urlSegment1;

$out = "";
$out .= "{$pages->get($id)->title}<br/>";
$out .= "{$pages->get($id)->body}<br/>";
$out .= "PRICE: {$pages->get($id)->discount_price}<br/>";

$out .= "
<form action='{$config->urls->root}checkout' method='post'>
<input type='text' name='product' value='{$pages->get($id)->title}'>
<input type='text' name='price' value='{$pages->get($id)->discount_price}'>
<input type='submit' value='Pay'>
</form>
";

$page->body = $out;

include('./main.php');

?>

checkout template

<?php

$out = '';

$out .= $input->post->product;
$out .= $input->post->price;

$out .= "Submitted data";

$page->body .= $out;

include('./main.php');

When order is submitted to checkout page,  no post values are got.

Anything wrong with my code ?

ps I'm testing on 2.5.3

Link to comment
Share on other sites

action='{$config->urls->root}checkout'

Isn't your checkout page end with a slash? (yoursite.com/checkout/)

If so, you may have a redirect so post values are lost (checkout -> checkout/), so you need to append a slash to the form action.

Check it on the Dev Tools/Firebug Network tab.

  • Like 2
Link to comment
Share on other sites

Yep the problem is the one tpr describes. Beside that, you should consider the following:

Don't build your urls manually, let ProcessWire do it for you:

$action = $pages->get('/checkout/')->url;
<form action="$action">

Sanitize input

$id = (int) $input->urlSegment1; // Must be an integer

Cache the page you're searching; I'm sure that ProcessWire also returns a cached page, but in theory, each $pages->get() call fires an SQL query:

$myPage = $pages->get($id); // Assign the found page to variable $myPage
if ($myPage->id) {
  // Make sure we found a page
  echo $myPage->title;
}

Escape output

$title = $sanitizer->entities($myPage->title);
echo "<input type='text' name='product' value='{$title}'>";

Edit: This is only needed if your title field does not have the "HTML Entitiy Decoder" textformatter applied.

Cheers

  • Like 8
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...