Jump to content

Site Won't Submit Multiple Variables


BobJohnson

Recommended Posts

Looking at what you just posted:

//First Page with Selector//    <form id="main-search-form" method="GET" action="<?= $baseurl; ?>/_searchresults6.php" role="search">

The first line appears to be a PHP comment line - certainly not an HTML comment.  Therefore your next line is confusing since it seems to be plain html that includes some php code which has to cause a syntax error at some point

PS - why a GET? Most of the time it is better to use POST for retrieving form data.

PS - your error messages refer to the use of "explode".  I can't find an "explode" call in any of this code?  Wassup?

Link to comment
Share on other sites

7 minutes ago, ginerjm said:

PS - why a GET? Most of the time it is better to use POST for retrieving form data.

Use GET when you want to get data from the server to display.

Use POST when sending something to the server, for example, to update a record.

Exceptions would be when using sensitive data (eg passwords) or large amounts of data since GET has limit of around 2K. (Fuller explanation)

Link to comment
Share on other sites

Try this. 

<form id="main-search-form" method="POST" action="<?= $baseurl; ?>/_searchresults.php" role="search">
          <select id="query-input" name="city_id" style="width:28.8%" style="height:69px" required>
              <option value="">Select Area</option>
              <option value="3,1">Ches & VB</option>
              <option value="3">Chesapeake , VA</option>
              <option value="9">Hampton , VA</option> 
              <option value="10">Newport News , VA</option>   
              <option value="2">Norfolk , VA</option>   
              <option value="12">Poquoson , VA</option> 
              <option value="4">Portsmouth , VA</option>    
              <option value="5">Suffolk , VA</option>    
              <option value="1">Virginia Beach , VA</option> 
              <option value="11">Williamsburg , VA</option>
              <option value="0">All Active US Cities</option>
        </select>

then 

if($_SERVER['REQUEST_METHOD'] == 'POST'){
	if(isset($_POST['city_id']) && $_POST['city_id']!="") {
		$city_ids=explode(",", $_POST['city_id']); 
		$query = "SELECT
			city_name, 
			state 
			FROM cities"; 
		switch($_POST['city_id']) { 
			case 0: 
			Default: 
			$query.=" WHERE city_id IN (:" . implode(',:',$city_ids) . ")";
		}
    	$stmt = $conn->prepare($query);

		foreach($city_ids as $city_id) { 
			$stmt->bindValue(":query_city_id",$city_id); 
		}
    	$stmt->execute();    
		$cities = $stmt->fetchAll(PDO::FETCH_ASSOC);
    	foreach($cities as $city) { 

		}
	}
}

Haven't tested it. But something like this. 

Link to comment
Share on other sites

This should be close to what you need

<?php
if (isset($_GET['city_id'])) {
    $where = [ "p.status != 'trashed'",  "paid = 1" ];
    $whereclause = '';
    $params = [];
    if ($_GET['city_id'] != 0) {
        $ids = explode(';', $_GET['city_id']) ;        // split multiple ids
        $k = count($ids);
        $placeholders = array_fill(0, $k, '?');
        $placestr = join(',', $placeholders);
        $where[] = "city_id IN ($placestr)";
        $params = $ids;
    }
    if (trim($_GET['query']) != '') {
        $where[] = "MATCH(place_name, description) AGAINST (?)";
        $params[] = $_GET['query'];
    }
    $whereclause = "WHERE " . join(' AND ', $where);
    
    // FIND HOW MANY RECORDS FOR PAGINATION
    $res = $conn->prepare("SELECT COUNT(*)
                         FROM cities c
                         JOIN places p USING (city_id)
                         $whereclause
                        ");
    $res->execute( $params );
    $total_rows = $res->fetchColumn();
    
    // NOW GET THE RECORDS FOR DISPLAY
    $rows_per_page = 10;
    $total_pages = ceil($total_rows/$rows_per_page);
    $page = $_GET['page'] ?? 1;
    $offset = ($page - 1) * $rows_per_page;
    
    $res = $conn->prepare("SELECT c.city_name
                              , c.state
                              , p.place_id
                              , p.place_name
                              , p.lat
                              , p.lng
                              , p.description
                          FROM cities c
                          JOIN places p USING (city_id)
                          $whereclause
                          ORDER BY city_name, place_name
                          LIMIT $offset, $rows_per_page
                        ");
    $res->execute( $params );
    
    // OUTPUT  YOUR RESULTS HERE
}
?>
<hr>
<form>
City <select id="query-input" name="city_id" required>
        <option value="">Select Area</option>
        <option value="3;1">Ches & VB</option>
        <option value="3">Chesapeake , VA</option>
        <option value="9">Hampton , VA</option>    
        <option value="10">Newport News , VA</option>            
        <option value="2">Norfolk , VA</option>            
        <option value="12">Poquoson , VA</option>    
        <option value="4">Portsmouth , VA</option>                
        <option value="5">Suffolk , VA</option>                
        <option value="1">Virginia Beach , VA</option>    
        <option value="11">Williamsburg , VA</option>
        <option value="0">All Active US Cities</option>
        </select>
<br>        
Search for <input type="text" name="query" value="">
<input type="submit" name="btnSub" value="Search">
<br>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.