Search Form with One or More (Multiple) Parameters

前端 未结 2 2095
遇见更好的自我
遇见更好的自我 2020-11-22 04:05

I\'ve gotten the basics down, where I\'ve created two files, the search form where a user inputs search parameters, and the results file that cranks out inputted items. For

2条回答
  •  执念已碎
    2020-11-22 04:33

    If you aren't going to change anything in the database - you are just selecting - go ahead and use GET instead of POST. The advantage of this is that it is going to allow you to save the URL as your search string. You can also refresh the search without getting the resubmit post alert. You just want to make sure that you parameterize your values before you send them to the database. I would normally send those values through sanitize functions, such as a regex that makes sure you only have letters if you expect letters, or a numbers if you expected numbers.

    On the same page (all search): (I am just going to outline this for you.)

    " method="GET"> " />
    prepare($sql); $stmt->execute($params); } ?>

    Now you can display your data.

    edit: I wrote the other half of the answer, and then he wrote the 2nd half, so I just incorporated it...

    Also, the next level of sophistication in this would be to take the PHP out of the search file and to put it into another file. When you press the search button in your form, you'd use AJAX to call the PHP elements. Then the PHP file would return the results via Ajax. You could return either the HTML preformatted, or JSON and let something like JQuery display it for you.

提交回复
热议问题