Hi All
Please somehelp me to correct my code so that after the user click the submit button it redirects to the correct URL if the value is correct. I get the error when pressing submit this php file will be displayed in an iframe
Error
Warning: Cannot modify header information - headers already sent by
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$amountErr = "";
$amount = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (($_POST["amount"]) < 250) {
$amountErr = "Value must be equal or greater than 250";
} else {
$amount = test_input($_POST["amount"]);
header('location:https://www.moneybookers.com/app/payment.pl');
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
<p><span class="error">*required field.</span></p>
Amount:<input type="text" name="amount"/>
<span class="error">*<?php echo $amountErr; ?></span><bR>
<input type="hidden" name="pay_to_email" value="someone.com"/>
<input type="hidden" name="status_url" value="someone.com"/>
<input type="hidden" name="return_url" value="http://wwww.somesite.com"/>
<input type="hidden" name="language" value="EN"/>
<input type="hidden" name="currency" value="USD"/>
<input type="hidden" name="detail1_description" value="New Deposit"/>
<input type="hidden" name="detail1_text" value="New Deposit"/><BR>
<input type="submit" value="Deposit!"/>
In this stage you have to use javascript /jQuery instead of php. Here is replacement for your code.
<script type="text/javascript">
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function validateForm(obj) {
if (obj.amount.value == '') {
document.getElementById("error").innerHTML = 'Please enter any amount!';
return false;
}
if (!isNumber(obj.amount.value) && obj.amount.value < 250) {
document.getElementById("error").innerHTML = 'Value must be equal or greater than 250';
return false;
}
return true;
}
</script>
<form method="post" action="https://www.moneybookers.com/app/payment.pl" onsubmit="return validateForm(this);">
<p><span class="error">*required field.</span></p>
Amount:<input type="text" name="amount"/>
<span class="error" id="error"></span><bR>
<input type="hidden" name="pay_to_email" value="someone.com"/>
<input type="hidden" name="status_url" value="someone.com"/>
<input type="hidden" name="return_url" value="http://wwww.somesite.com"/>
<input type="hidden" name="language" value="EN"/>
<input type="hidden" name="currency" value="USD"/>
<input type="hidden" name="detail1_description" value="New Deposit"/>
<input type="hidden" name="detail1_text" value="New Deposit"/><BR>
<input type="submit" value="Deposit!"/>
SamA74
December 19, 2013, 10:38am
3
I think the header redirect only works before the HTML <head>
If you cut and paste that chunk of PHP to the very beginning of your document, it should work.
Forget about the iframe and use jQuery load . Make the submit into the main page so, action=“” (you don’t need action=“<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]); ?>” - this is a bad practice, for many reasons). Also, make sure that the PHP code is in the first place , no spaces before it, no output. The file must begin with
<?php
if( stuff ) {
header('Location: page.php');
exit; // do not forget about exit
}
?> Here you have
Your HTML code and other output
tamilsuresh:
In this stage you have to use javascript /jQuery instead of php. Here is replacement for your code.
<script type="text/javascript">
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function validateForm(obj) {
if (obj.amount.value == '') {
document.getElementById("error").innerHTML = 'Please enter any amount!';
return false;
}
if (!isNumber(obj.amount.value) && obj.amount.value < 250) {
document.getElementById("error").innerHTML = 'Value must be equal or greater than 250';
return false;
}
return true;
}
</script>
<form method="post" action="https://www.moneybookers.com/app/payment.pl" onsubmit="return validateForm(this);">
<p><span class="error">*required field.</span></p>
Amount:<input type="text" name="amount"/>
<span class="error" id="error"></span><bR>
<input type="hidden" name="pay_to_email" value="someone.com"/>
<input type="hidden" name="status_url" value="someone.com"/>
<input type="hidden" name="return_url" value="http://wwww.somesite.com"/>
<input type="hidden" name="language" value="EN"/>
<input type="hidden" name="currency" value="USD"/>
<input type="hidden" name="detail1_description" value="New Deposit"/>
<input type="hidden" name="detail1_text" value="New Deposit"/><BR>
<input type="submit" value="Deposit!"/>
Hi Thank you for responding. I have tried the above but the javascript code is not validating or getting called