Jump to content

How do I make a link go to a specific search, I'm new to this and I'm making a project


Vaske
Go to solution Solved by Barand,

Recommended Posts

I wasn't to make a link go to a specific search but I cant seem to make it work, I would appreciate any help here!

 

Here is a part of the main page:

Categories
        </a>
        <div class="dropdown-menu" style="background-color: black;" aria-labelledby="navbarDropdown">
          <a style="color: green;" class="dropdown-item" href="search.php?search=action">Action</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=comedy">Comedy</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=drama">Drama</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=fantasy">Fantasy</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=horror">Horror</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=mistery">Mistery</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=romance">Romance</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=thriller">Thriller</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=western">Western</a>
          <a style="color: green;" class="dropdown-item" href="search.php?search=scifi">SciFi</a>
        </div>
      </li>

And here is the search.php:

 

<?php
include("db.php");
include("header.php");
include("footer.php");
?>

<div class="row">
<?php

if(isset($_POST['submit'])){
	$search = $_POST['search'];
	$searchpreg = preg_replace("#[^0-9a-z]#i", "", $search);
	
	$query = "SELECT * FROM movie WHERE name LIKE '%$search%' or genre LIKE '%$search%'";
	$run = mysqli_query($con,$query);
	$count = mysqli_num_rows($run);
	if($count == 0){
		echo "<div style='text-align:center;'><h1>No Movie Found</h1>".$search."</div>";
	}
	else{
		while($row = mysqli_fetch_assoc($run)){
			?>
			<div class="col">
				<div class="card border border-success background-color:" style="width:200px; text-align:center; background-color:#343a40;">
			<a href="viewmovie.php?id=<?php echo $row['id']; ?>"><?php echo "<img height='180px' width='180' src='admin12312/thumb/".$row['imgpath']."'>"; ?></a>
			<p style="color: green;"><?php echo $row['name']; ?></p>
			</div>
			</div>
			<?php
			
			
		}
	}
}

?>

</div>

 

Link to comment
Share on other sites

32 minutes ago, ginerjm said:

$_GET['search']

Sorry to bother you, but I'm completely new to this and have been following a tutorial so far and I think I learned quite a bit, but some things I still need help with. Where do I put the $_GET['search'] and how do I use it?

Link to comment
Share on other sites

Use $_GET['search'] instead of $_POST['search'].

If the search=??? is in the url query string, the key/value pair will be put into the GET array.

For those times when you are just getting data (as in a search) use GET. If updating something, use POST

Edited by Barand
  • Like 1
Link to comment
Share on other sites

20 minutes ago, Barand said:

Use instead of $_POST['search'].

If the search=??? is in the url query string, the key/value pair will be put into the GET array.

For those times when you are just getting data (as in a search) use GET. If updating something, use POST

I tried replacing the $_POST['search'] with $_GET['search'] but I end up getting undefined index: search

Link to comment
Share on other sites

16 minutes ago, Barand said:

Have you got

if(isset($_POST['submit'])){

or

if(isset($_GET['search'])){

 

ok so I did this and it works thank you! But now the search bar doesnt work at all. Any way to make both of them work together? Maybe I should make 2 search.php files one for each instance?

Link to comment
Share on other sites

10 minutes ago, Barand said:

[correction to question]

Honestly I have no idea, it just doesn't work when I change it to GET , but it doesnt matter to me much it works fine when I have 2 search pages so I'm fine with that. If it works I don't mind

Link to comment
Share on other sites

It wouldn't hurt to learn a bit about the basics of HTTP protocol.

  • GET Method passes all parameters on the URL.
  • POST Method passes parameters in the request body.

This is 2 totally different ways of passing data from a form to the "target"/receiving script.

PHP hides complexity in its page processing by bundling up the passed url parameters ($_GET) or variables in the request body ($_POST), but in either case, you need to understand the method the form used to call the script that is receiving data.  

If a form tag doesn't specify a method, data from the form will be passed using the GET method.  Conversely, if you specify the method of the form to be either GET or POST, then you will want to check either $_GET or $_POST.

In PHP there is actually a sort of cheat, which is to check $_REQUEST, which php builds with the same data regardless of whether the data was sent by GET or POST.

<form action="somepage.php" method="get">
  <label for="fname">Search:</label>
  <input type="text" id="search" name="search"><br><br>
  <input type="submit" value="Submit">
</form>

vs

<form action="somepage.php" method="post">
  <label for="fname">Search:</label>
  <input type="text" id="search" name="search"><br><br>
  <input type="submit" value="Submit">
</form>

Notice the method?  The reason it didn't work when you checked $_GET[] was that your form had method="post" set.  

Creating 2 identical scripts to do searches, because you didn't understand this very basic concept in regards to how html forms work, is not a good way to maintain or enhance your system.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.