javascript - Fetch from MySQL with AJAX -
i'm building web application connects database has got couple of variables stored that's necessary site function properly.
right i'm using php connect , fetch information in end easier use .html file, i'm planning on using ajax fetch information instead.
i've got code:
$(function () { $.ajax({ url: 'api.php', data: { paramname: $("#partyidinput").val() }, method: post, datatype: 'json', success: function(data) { var id = data[0]; var pid = data[1]; var ploc = data[2]; var pname = data[3]; var pformid = data[5]; var pformsong = data[6]; var pformartist = data[7]; var pformname = data[8]; $('#partytitle').append(""+pname+""); } }); }); which fetches variables via api.php looks this:
<?php $db = new pdo('mysql:host=xxxxx;dbname=xxxxx;charset=utf8', 'xxxxx', 'xxxxx'); $partyinput = $_post['paramname']; $stmt = $db->prepare("select * playr_partyid_db partyid = $partyinput"); $stmt->execute(array($_post['paramname'])); $rows = $stmt->fetchall(pdo::fetch_assoc); ?> but problem i've got text input on web page, when user writes "partyid" in , clicks continue want script fetch information row id. it's fetching 1st row.
i've never worked ajax before if you've got time, please explain what's happening too.
thanks in advance.
edit:
here's input, note haven't optimized page in terms of css yet.
<form style="margin-top:150px;" method="post" action=""> <div class="list-block"> <ul> <li class="item-content" style="background: #fff;margin-bottom: -26px;"> <div class="item-inner"> <div class="item-input"> <input style="text-align:center;padding:0;" type="text" name="partyid" placeholder="ange partyid"> </div> </div> </li> </ul> </div> <div class="list-block"> <ul> <li> <input type="submit" name="submit" value="festa på!" class="login_bttn" style=" background: none;color: white;border: 1px solid white;border-radius: 500px;width: auto;margin-left: auto;margin-right: auto;margin-top: 40px;"></input> </li> </ul> <div class="list-block-labe"></div> </div> </form>
first of all, pass parameter, update data: "",
data: { paramname: $("#idoffield").val() }, secondly, jay blanchard mentioned, switch pdo
$db = new pdo('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password'); $stmt = $db->prepare("select * playr_partyid_db partyid =?"); $stmt->execute(array($_post['paramname'])); $rows = $stmt->fetchall(pdo::fetch_assoc);
Comments
Post a Comment