1

When I try to access cookies using inline php, my submit disappears for whatever reason.

    <div id='add_form'>
        <form id='form' method='post' action='reg.do.php'>
            <label for='date'>Date of Charge:</label><input type='text' name='date' id='date' maxlength='10' value='<?php echo (isset($_POST['name']) ? sanitize($_POST['name']) : ''); ?>' ><br>
            <label for='vendor'>Vendor:</label><input type='text' name='vendor' id='vendor' value='<?php echo (isset($_POST['vendor']) ? sanitize($_POST['vendor']) : ''); ?>' ><br>
            <label for='amount'>Amount:</label><input type='text' name='amount' id='amount' value='<?php echo (isset($_POST['amount']) ? sanitize($_POST['amount']) : ''); ?>' ><br>
            <label for='service'>Description of Service:</label><input type='text' name='service' id='service' value='<?php echo (isset($_POST['service']) ? sanitize($_POST['service']) : ''); ?>' ><br>
            <label for='winner'>Winner:</label><input type='text' name='winner' id='winner' value='<?php echo (isset($_POST['winner']) ? sanitize($_POST['winner']) : ''); ?>' ><br>
            <label for='job'>Job Number:</label><input type='text' name='job' id='job' value='<?php echo (isset($_POST['job']) ? sanitize($_POST['job']) : ''); ?>' ><br>
            <input type='hidden' name='user' id='user' value='<?php echo (isset($_COOKIE['cookie_id']) ? sanitize($_COOKIE['cookie_id']) : ''); ?>' >
            <input type='submit' value='Submit'>
        </form>
    </div>

If I get rid of that last hidden input field or change the $_COOKIE access inside of it to $_POST, then the submit button will show up. How can I fix this?

2
  • 1
    Are you getting any single quotes or apostrophe's back from your cookie values? That would break the html attribute, and possibly cause the next item to display incorrectly (or not at all).
    – wwwmarty
    Sep 22, 2014 at 19:55
  • I shouldn't as the $_COOKIE['cookie_id'] should be returning a first and last name that doesn't have any special characters.
    – Link
    Sep 22, 2014 at 20:04

2 Answers 2

1

See http://php.net/manual/ro/reserved.variables.cookies.php, I think you need to use the htmlspecialchars function on the cookie.

3
  • The sanitize function includes htmlspecialchars in its filtering process, so it is going through the function before being outputted.
    – Link
    Sep 22, 2014 at 20:02
  • Could you copy paste the div code from the page source?
    – Mandera
    Sep 22, 2014 at 20:44
  • If you open your page, right click and press "Show page source" or something similar, and then do ctrl+f, enter '<div id='add_form'>' and then copy paste that section.
    – Mandera
    Sep 22, 2014 at 20:49
0

have you enabled display_errors? maybe you have an error in your code so php breaks, like an unclosed ) in your inline IF condition ;-)

try this...

<div id='add_form'>
    <form id='form' method='post' action='reg.do.php'>
        <label for='date'>Date of Charge:</label><input type='text' name='date' id='date' maxlength='10' value='<?php echo (isset($_POST['name']) ? sanitize($_POST['name']) : ''); ?>' ><br>
        <label for='vendor'>Vendor:</label><input type='text' name='vendor' id='vendor' value='<?php echo (isset($_POST['vendor']) ? sanitize($_POST['vendor']) : ''); ?>' ><br>
        <label for='amount'>Amount:</label><input type='text' name='amount' id='amount' value='<?php echo (isset($_POST['amount']) ? sanitize($_POST['amount']) : ''); ?>' ><br>
        <label for='service'>Description of Service:</label><input type='text' name='service' id='service' value='<?php echo (isset($_POST['service']) ? sanitize($_POST['service']) : ''); ?>' ><br>
        <label for='winner'>Winner:</label><input type='text' name='winner' id='winner' value='<?php echo (isset($_POST['winner']) ? sanitize($_POST['winner']) : ''); ?>' ><br>
        <label for='job'>Job Number:</label><input type='text' name='job' id='job' value='<?php echo (isset($_POST['job']) ? sanitize($_POST['job']) : ''); ?>' ><br>
        <input type='hidden' name='user' id='user' value='<?php echo ( isset($_COOKIE['cookie_id']) ) ? sanitize($_COOKIE['cookie_id']) : ''; ?>' >
        <input type='submit' value='Submit'>
    </form>
</div>

by the way... it's better to use some kind of template engines instead of inline php code.

1
  • They are enabled, and all the bracket and parenthesis pairing seems fine.
    – Link
    Sep 22, 2014 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.