javascript prompt sent to php but output empty -
i trying allow adding of category category dropdownlist clicking '+' button below using ajax dropdownlist keeps disappearing instead.
html codes follows
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <script> function addcategory() { var category = prompt("please enter new category: ", ""); if (category != null){ if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else { xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4){// && xmlhttp.status==200) { document.getelementbyid("category").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","add_category.php?category="+category,true); xmlhttp.send(); } } </script> <script language="javascript" src="calendar/calendar.js"></script> </head> <body> <?php include ('db_conn.php'); session_start(); if(!empty($_request['event'])){ $event = $dbc->prepare("select * `coo_def_event` event_id = :eventid;"); try{ $event->bindparam(':eventid', $_request['event']); $event->execute(); $eventdet = $event->fetch(pdo::fetch_assoc); }catch(pdoexception $ex){ echo 'error getting event data'; } echo '<form id="form1" name="form1" method="post" action="editevent.php">'; }else{ echo '<form id="form1" name="form1" method="post" action="addevent.php">'; } ?> category: <div id="category"><select name="categorydpl" id="categorydpl"> <?php $categorysql = $dbc->prepare("select * `category` user_id = :userid; "); try{ $categorysql->bindparam(':userid', $_session["userid"]); $categorysql->execute(); $categorylist = $categorysql->fetchall(pdo::fetch_assoc); foreach ($categorylist $category){ echo '<option value="'.$category['category_id'].'">'.htmlspecialchars($category['category_name']).'</option>'; } }catch(pdoexception $ex){ echo 'error getting data'; } ?> </select></div><button onclick="addcategory()">+</button> <p> <input type="submit" name="btnsubmit" id="btnsubmit" value="submit" /><button onclick="location.href ='index.php';">cancel</button> </form> </body> </html>
php file
<?php include ('db_conn.php'); session_start(); $category = $_get['category']; $print='category entered: '.$category; $sql = $dbc->prepare("insert `coo_category` (`user_id`, `category_name`) values (:userid, :category_name);"); try{ $sql->bindparam(':userid', $_session["userid"]); $sql->bindparam(':category_name', $category); $sql->execute(); }catch (pdoexception $ex){ echo 'insertion failed. please try again'; } $categorysql = $dbc->prepare("select * `coo_category` user_id = :userid;"); try{ $categorysql->bindparam(':userid', $_session["userid"]); $categorysql->execute(); $categorylist = $categorysql->fetchall(pdo::fetch_assoc); $print .= '<select name="categorydpl" id="categorydpl">'; foreach ($categorylist $category){ $print.= '<option value="'.$category['category_id'].'">'.htmlspecialchars($category['category_name']).'</option>'; } $print.='</select>'; }catch(pdoexception $ex){ echo 'error getting data'; } echo $print; ?>
when open php typing .../add_category.php?category=sad
the page display
"category entered: sad " followed dropdown list sad inserted.
but when try html file, dropdownlist disappear after click plus button , enter value.
any advice?
thanks in advance!!!
submit buttons submit forms. submitting form reload page.
use <button type="button">
Comments
Post a Comment