SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Page does not redirect with header

    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

    Code:
            <?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!"/>

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Perundurai, Tamilnadu, India
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this stage you have to use javascript /jQuery instead of php. Here is replacement for your code.

    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!"/>

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tamilsuresh View Post
    In this stage you have to use javascript /jQuery instead of php. Here is replacement for your code.

    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

  5. #5
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
    <?php
    
    if( stuff ) {
    
        header('Location: page.php');
        exit; // do not forget about exit
    }
    ?> Here you have
    Your HTML code and other output
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •