php - $(form id).serialize() returns empty string -
i have simple search form content want send php query post
request. i'm using ajax send it, , far works fine, reason instead of sending actual value of input field, sends empty string.
can please explain i'm doing wrong here?
my html:
<form id="searchbox"> <input type="text" placeholder="author, title, keyword..."> <input type="submit" value="search"> </form>
my js (the console.log
line there in order see what's getting posted, ie checking what's wrong code):
$("#searchbox").submit(function(e) { e.preventdefault(); console.log($("#searchbox").serialize()); $.post( "db_queries/all_articles.php", $( "#searchword" ).serialize(), function(data) { console.log(json.parse(data)); } //end response function ); //end search article post request })
my php:
try { $hostname = "localhost"; $username = "root"; $password = ""; $db = new pdo("mysql:host=$hostname;dbname=topdecka_ptc",$username, $password); if (!empty($_post)) { $query = $db->prepare('select * articles title = :title'); $query->execute(array(':title' => 'test1')); $result = $query->fetchall(pdo::fetch_assoc); echo json_encode($result); die(); } else { $query = $db->prepare('select * articles'); $query->execute(); $result = $query->fetchall(pdo::fetch_assoc); echo json_encode($result); die(); } } catch (pdoexception $e) { echo "error!: " . $e->getmessage() . "<br/>"; die(); }
your input tags must have name
attribute in order serialized.
Comments
Post a Comment