0

I am trying to pass the link's text as a value to the next page so I can use it to search the database for the item and retrieve the information related to the value .I have tried using the POST method but regardless the information is not passed. This is the code I tried .

<form action="DetailedMenu.php"  method = "POST" action = "<?php $_PHP_SELF ?>">

<?php

for($i=0;$i<sizeof($array);$i++) {
    if($array[$i]["Food_Category"]=="starters") {
        echo str_repeat('&nbsp;', 4); ?>

        <a  href="DetailedMenu.php" ><?php echo $array[$i]["Food_Name"];?></a>

        <?php echo "   " .str_repeat('.&nbsp;', 25). "€".$array[$i]["Food_Price"]."<br>"; ?>

        <input type="hidden" name="name" value="<?php echo $array[$i]["Food_Name"];?>">

        <?php
    }
}

?>

</form>
icecub
  • 8,615
  • 6
  • 41
  • 70
Cz147
  • 11
  • I forgot to mention that I retrieve all the items from the list and store them inside an array and loop through it to print the required items. – Cz147 Apr 14 '16 at 21:44
  • What does your html source look like on this page before form submit? And how are you submitting the form? I don't see a submit – Jeff Puckett Apr 15 '16 at 01:06

2 Answers2

0

You don't need the form.

The easiest way to do what you're trying to do....

In addition to including the text in the content of the link, include it as a query string parameter.

for($i=0;$i<sizeof($array);$i++) {
    if($array[$i]["Food_Category"]=="starters") {
        ...
        <a  href="DetailedMenu.php?q=<?php echo $array[$i]["Food_Name"];?>"><?php echo $array[$i]["Food_Name"];?></a>
        ...
    }
}

I would actually recommend something more like this. I obviously don't know the names of your fields, so I've just taken a guess...

for($i=0;$i<sizeof($array);$i++) {
    if($array[$i]["Food_Category"]=="starters") {
        ...
        <a  href="DetailedMenu.php?FoodID=<?php echo $array[$i]["Food_ID"];?>"><?php echo $array[$i]["Food_Name"];?></a>
        ...
    }
}

You'll be able to access "FoodID" as a parameter within your PHP, just as you would if it had been submitted from a form.

user1751825
  • 4,029
  • 1
  • 28
  • 58
  • Could you elaborate a bit more ? I didn't quite understand what you mean – Cz147 Apr 14 '16 at 21:54
  • 1
    Please refrain from stating "facts" that aren't true. Just because it's more common to use `GET` for search purposes, doesn't mean you're supposed to do it. Using `POST` instead is perfectly fine as well. – icecub Apr 14 '16 at 21:56
  • The text you want to send to DetailedMenu.php needs to be included in the actual URL (href). The link text is not sent. Sorry I can't write the code as I'm using a phone. – user1751825 Apr 14 '16 at 21:59
  • @icecube What I stated is correct. It is incorrect to use post method for a search. Just because it works doesn't mean it's correct. Look up what these methods mean. – user1751825 Apr 14 '16 at 22:01
  • 1
    Actually, for a search engine, you're correct. Namely because POST will re-submit the data when the user tries to navigate back. However, I doubt this is a search engine. Instead it's most likely just a hyperlink towards a more detailed page about a certain menu. – icecub Apr 14 '16 at 22:10
  • @Cz147 I've updated my answer with code, now that I'm at my workstation. Please excuse if the escaping isn't correct, as I don't have a way to test PHP at the moment. However I think this should get you very close. Actually rather than including the "Food_Name" in the link, you might want to use an ID field instead. – user1751825 Apr 14 '16 at 22:34
  • @icecub General rule.. Retrieval = GET. Update = POST. Get requests are idempotent. They can be safely run multiple times without altering the data in the system. Post requests usually aren't idempotent, as they POST content to the system. Adding a new type of food to the database, for example. Running it multiple times results in duplicated content. While you can use these methods in ways they're not intended to be used, it typically causes poor usability (inability to bookmark search results, or detail screens etc.) and causes confusion for other programmers maintaining your work. – user1751825 Apr 14 '16 at 22:41
  • @gibberish If I was reviewing your code, I would defect it for this. Just saying. I really really do know what I'm talking about. Competent programmers DO NOT use post exclusively. Programmers who do not understand HTTP use post exclusively. – user1751825 Apr 14 '16 at 23:01
  • @gibberish 'POST' isn't some new upgraded version of 'GET'. Both have a specific purpose, and should be used in specific ways. They are not interchangeable. – user1751825 Apr 14 '16 at 23:07
  • @user1751825 I rescind my query regarding your knowledge level. You certainly have the confidence of a Douglas Crawford, perhaps you also have his experience. In the real world, and for the OP's present purposes, I doubt the `post` / `get` dichotomy is worth much consideration. But I am sure your comments are correct. As for real-world application, both `
    ` and AJAX are appropriate solutions. However, I wished to present the OP with the opportunity to consider AJAX. Sometimes an answer is intended to suggest another approach, rather than to resolve the specific question that was asked.
    – cssyphus Apr 14 '16 at 23:28
-1

You may be looking for AJAX. AJAX lets you send the form data to a back end PHP file (that can then insert data into a DB, and/or get data from the DB) without refreshing the page.

In fact, when you are using AJAX you don't even need to use a <form> structure -- simple DIVs work just fine. Then you don't need to use event.preventDefault() to suppress the built-in form refresh.

Just build a structure inside a DIV (input fields, labels, etc) and when the user is ready to submit, they can click an ordinary button:

<button id="btnSubmit">Submit</button>

jQuery:

$('#btnSubmit').click(function(){
    var fn = $('#firstname').val();
    var ln = $('#lastname').val();
    $.ajax({
        type: 'post',
         url: 'ajax_receiver.php',
        data: 'fn=' +fn+ '&ln=' +ln,
        success: function(d){
            if (d.length) alert(d);
        }
    });
});

ajax_receiver.php:

<?php
    $fn = $_POST['fn'];
    $ln = $_POST['ln'];

    //Do your stuff

Check out this post and especially its examples. Copy them onto your own system and see how they work. It's pretty simple.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • This doesn't answer the question. The question was regarding sending the anchor content to the PHP script. Your sample is just a general ajax example, which doesn't attempt to address this. There is nothing in the question which suggests ajax could or should be used. – user1751825 Apr 14 '16 at 22:45
  • And that is exactly what I intended to provide. I was less interested in answering the OP's stated question than in helping them to create a simple, elegant solution. AJAX is a valid option, worthy of consideration, regardless whether the OP chooses to use it. – cssyphus Apr 14 '16 at 23:00
  • AJAX in this case does nothing more than complicate the problem. What does it solve in this case that couldn't be solved using a normal request? How does it solve the issue of getting the content from the anchor link? – user1751825 Apr 14 '16 at 23:05
  • Having submitted his content to an ajax receiver, then what? It still has to be displayed to the end user. – user1751825 Apr 14 '16 at 23:10
  • The OP's application is a simple AJAX lookup. The [examples that I linked in my answer](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843) provide code examples of how to `echo` data back from the PHP file, and how to receive/use them on the HTML side. – cssyphus Apr 14 '16 at 23:26
  • The OP hasn't mentioned AJAX anywhere. – user1751825 Apr 14 '16 at 23:27
  • So...? Perhaps they never thought of using it. I didn't answer this question for an upvote or checkmark, but to help with a solution. *Over the years many persons on this site have helped me out in the same way -- and I often didn't understand their response until later. However, if they hadn't planted that seed, who knows how much longer it would have taken to discover their approach...* – cssyphus Apr 14 '16 at 23:34
  • I do understand that. However in this case, where the OP seems to be a relatively inexperienced programmer, this could simply confuse him, and make him think he needs to implement a whole different design methodology to solve one very tiny problem, which can be solved in a much simpler way. – user1751825 Apr 14 '16 at 23:38
  • Re-reading the question, I note that the OP desires to (1) send information to a back-end file, (2) back-end file uses that information to perform a database search, and (3) `retrieve the information related to the value`. That final criterium is best (and most simply) resolved via AJAX, imho. Honestly, studying the examples I provided (linked), after 30 mins study/experimentation the OP could have an AJAX solution. And does anyone ever return to PHP forms after using AJAX...? – cssyphus Apr 15 '16 at 00:28