Home > OS >  How to redirect a form to the same page while it's being echoed out
How to redirect a form to the same page while it's being echoed out

Time:03-22

I'm trying to redirect a form to the same page typically when a form is displayed to the screen using HTML you can just nest PHP into the form like this

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

but I'm echoing out a form in this use case and I'm receiving errors due to the apostrophes and I can't figure out how to get it working

echo "
        <h1>Update '{$rowObj->first_name}'</h1>
            <form id='updateuser' class='update' action='echo htmlspecialchars($_SERVER['PHP_SELF'])' method='post'>
            <p>Customer ID<input type='text' class='update' name='customer_id' value='$customer_id' readonly /></p>
            <p>Name <input type='text' class='update' name='first_name' size='50' value='{$rowObj->first_name}' /></p>
            <p>surname <input type='text' class='update' name='surname' size='50' value='{$rowObj->surname}' /></p>
            <p>Postcode <input type='text' class='update' name='postcode' size='50' value='{$rowObj->postcode}' /></p>
            <p>Address <input type='text' class='update' name='address' size='50' value='{$rowObj->address}' /></p>
            <p>Address <input type='text' class='update' name='email' size='50' value='{$rowObj->email}' /></p>
            <p>Phone number <input type='text'class='update'  name='number' value='{$rowObj->phonenumber}' /></p> 
            <p><input type='submit' class='submitbutton' name='submit' value='Confirm edit'></p>  ";

error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\test\edit.php on line 38

CodePudding user response:

If you're including variables inside of a string, you should remove the quotes used to reference the member of the variable from $_SERVER["PHP_SELF"] to $_SERVER[PHP_SELF].

The first one is fine since it is not included inside of a string:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

That should resolve the errors you're receiving:

echo "
        <h1>Update '{$rowObj->first_name}'</h1>
            <form id='updateuser' class='update' action='echo htmlspecialchars($_SERVER[PHP_SELF])' method='post'>
            <p>Customer ID<input type='text' class='update' name='customer_id' value='$customer_id' readonly /></p>
            <p>Name <input type='text' class='update' name='first_name' size='50' value='{$rowObj->first_name}' /></p>
            <p>surname <input type='text' class='update' name='surname' size='50' value='{$rowObj->surname}' /></p>
            <p>Postcode <input type='text' class='update' name='postcode' size='50' value='{$rowObj->postcode}' /></p>
            <p>Address <input type='text' class='update' name='address' size='50' value='{$rowObj->address}' /></p>
            <p>Address <input type='text' class='update' name='email' size='50' value='{$rowObj->email}' /></p>
            <p>Phone number <input type='text'class='update'  name='number' value='{$rowObj->phonenumber}' /></p> 
            <p><input type='submit' class='submitbutton' name='submit' value='Confirm edit'></p>  ";

CodePudding user response:

echo "
    <h1>Update '{$rowObj->first_name}'</h1>
    <form id='updateuser' class='update' action='echo 
    htmlspecialchars($_SERVER["PHP_SELF"])' method='post'>
     <p>Customer ID<input type='text' class='update' 
    name='customer_id' value='$customer_id' readonly /></p>
     <p>Name <input type='text' class='update' name='first_name' 
    size='50' value='{$rowObj->first_name}' /></p>
        <p>surname <input type='text' class='update' name='surname' 
size='50' value='{$rowObj->surname}' /></p>
        <p>Postcode <input type='text' class='update' name='postcode' 
size='50' value='{$rowObj->postcode}' /></p>
        <p>Address <input type='text' class='update' name='address' 
size='50' value='{$rowObj->address}' /></p>
        <p>Address <input type='text' class='update' name='email' 
 size='50' value='{$rowObj->email}' /></p>
        <p>Phone number <input type='text'class='update'  name='number' 
value='{$rowObj->phonenumber}' /></p> 
        <p><input type='submit' class='submitbutton' name='submit' 
 value='Confirm edit'></p>  ";
  •  Tags:  
  • php
  • Related