babygau
babygau

Reputation: 1571

Get variable from query string

I was trying to get variable in Query String from URL. But somehow, its just got one variable instead of getting all variables from querystring. I really don't know what goes wrong with my code. Here is the code I want to print out error from the invalidate form:

<?php       
            displayForm();
            function displayForm(){
                ?>                    
                <form action="./prod_add_action.php" method="post" name="addproductForm">
                    <fieldset>
                        <legend>Online Ordering System Setup</legend>                        
                        <label for="product_name">Product Name: </label><input type="text" name="product_name" value="" /><?php echo $_GET["name_error"]; ?>
                        <label for="product_date">Product Date: </label><input type="text" name="product_date" value="" /><?php echo $_GET["date_error"]; ?>
                        <label for="product_price">Product Price: </label><input type="text" name="product_price" value="" /><?php echo $_GET["price_error"]; ?>
                        <input name="add_button" type="submit" value="Add" />
                        <input name="reset_button" type="reset" value="Clear" />
                    </fieldset>
                </form>
                <?php
            }
            ?>

And here is the code I created the querystring:

$query_string = "name_error=" .urlencode($name_error) ."&amp;date_error=" .urlencode($date_error) ."&amp;price_error=" .urlencode($price_error);
            header("Location: ./prod_add.php?$query_string");
            exit();             

In the first code, the page only print the first $_GET['name_error'], while it should be include $_GET['date_error'] and $_GET['price_error. '] This is the address:

http://example.com/prod_add.php?name_error=Product+name+must+be+characters+only&date_error=Product+date+must+be+input+as+this+formate+DD-MM-YYYY&price_error=Product+price+must+be+float+number+only

Upvotes: 0

Views: 494

Answers (2)

Vishal
Vishal

Reputation: 2181

Change &amp; to & as:

$query_string = "name_error=" . urlencode($name_error) . "&date_error=" . urlencode($date_error) . "&price_error=" . urlencode($price_error);
header("Location: ./prod_add.php?$query_string");
exit();  

Upvotes: 1

iiro
iiro

Reputation: 3119

You should use & instead of &amp;'s ?

$query_string = "name_error=" .urlencode($name_error) ."&date_error=" .urlencode($date_error) ."&price_error=" .urlencode($price_error);
            header("Location: ./prod_add.php?$query_string");
            exit();   

Upvotes: 3

Related Questions