javascript - How to send data through ajax function to php file? -
i want call 1 addpost.php file through ajax function, , return result post submitted or not.
for calling useraction function onclick of button not getting data through post, when tried access post array data in addpost.php file sending error undefined index category.
can please going wrong?
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>post</title> </head> <body> <form class="postform" id="postform" method="post" action="addpost.html"> <fieldset> <legend>please add details below </legend> <p> <label for="title">title (required, @ least 2 characters)</label> <input id="title" name="title" minlength="2" type="text" required> </p> <p> <label for="url">url (required)</label> <input id="url" type="url" name="url"> </p> <p> <label for="desc">description (required, @ least 2 characters)</label> <input id="desc" name="desc" minlength="2" type="text" required> </p> <p> <label for="keywords">keywords (eg:#facebook)(required, @ least 2 characters)</label> <input id="keywords" name="keywords" minlength="2" type="text" required> </p> <p> select url type : <select name="urltype" id="urltype"> <option value="">select url type...</option> <option value="0">server image</option> <option value="1">server video</option> <option value="2">youtube video</option> <option value="3">vimeo video</option> <option value="4">facebook image</option> <option value="5">facebook video</option> <option value="6">instagram image</option> <option value="7">instagram video</option> <option value="-1">other</option> </select> </p> <p> select category : <select name="category" id="category"> </select> </p> <p> <input type="button" name="submit" id="submit" value="submit" onclick="useraction('add')"> </p> </fieldset> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> getcategories(); function getcategories() { $.ajax({ type: "post", url: 'getcategories.php', datatype: 'text', async: false, cache: false, success: function (result) { $('#category').html(result); } }); } function useraction(type,id){ id = (typeof id == "undefined")?'':id; var statusarr = {add:"added",edit:"updated",delete:"deleted"}; var userdata = ''; if (type == 'add') { userdata = $("#postform").find('.form').serialize() + '&action_type=' + type + '&id=' + id; } $.ajax({ type: 'post', url: 'addpost.php', data: userdata, success:function(report){ alert(report) } }); } </script> </form> </body> </html>
addpost.php
include 'database.php'; ini_set('display_errors', 1); error_reporting(1); ini_set('error_reporting', e_all); if(isset($_post['action_type']) && !empty($_post['action_type'])) { if($_post['action_type'] == 'add') { /* $database = new database(constants::dbhost, constants::dbuser, constants::dbpass, constants::dbname); $dbconnection = $database->getdb(); $dbconnection->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $dbconnection->prepare("insert keywords(keyword) values(?)"); $stmt->execute(array($_post['keywords'])); //insert data posts table $stmt = $dbconnection->prepare("insert posts(category_id,title,url,url_type,description,keywords) values(?,?,?,?,?,?)"); $stmt->execute(array($_post['category'], $_post['title'], $_post['url'], $_post['urltype'], $_post['desc'], $_post['keywords'])); $count = $stmt->rowcount(); if ($count > 0) { //if inserted $response = array("status" => -1, "message" => "post submitted"); return $response; } else { //if not inserted $response = array("status" => -1, "message" => "could not submit post."); return $response; }*/ echo $_post['category'], $_post['title'], $_post['url'], $_post['urltype'], $_post['desc'], $_post['keywords']; } }
edit :
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>post</title> </head> <body> <form class="postform" id="postform" method="post" action="addpost.php"> <fieldset> <legend>please add details below </legend> <p> <label for="title">title (required, @ least 2 characters)</label> <input id="title" name="title" minlength="2" type="text" required> </p> <p> <label for="url">url (required)</label> <input id="url" type="url" name="url" required> </p> <p> <label for="desc">description (required, @ least 2 characters)</label> <input id="desc" name="desc" minlength="2" type="text" required> </p> <p> <label for="keywords">keywords (eg:#facebook)(required, @ least 2 characters)</label> <input id="keywords" name="keywords" minlength="2" type="text" required> </p> <p> select url type : <select name="urltype" id="urltype"> <option value="">select url type...</option> <option value="0">server image</option> <option value="1">server video</option> <option value="2">youtube video</option> <option value="3">vimeo video</option> <option value="4">facebook image</option> <option value="5">facebook video</option> <option value="6">instagram image</option> <option value="7">instagram video</option> <option value="-1">other</option> </select> </p> <p> select category : <select name="category" id="category"> </select> </p> <p> <input type="hidden" name="action_type" id="action_type_id"/> <input type="hidden" name="id" id="p_id"/> <input type="button" name="submit" id="submit" value="submit" onclick="useraction('add')"> </p> <p id="report"></p> </fieldset> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> getcategories(); $("#postform").validate(); function getcategories() { $.ajax({ type: "post", url: 'getcategories.php', datatype: 'text', async: false, cache: false, success: function (result) { $('#category').html(result); } }); } function useraction(type,id){ var statusarr = {add:"added",edit:"updated",delete:"deleted"}; if (type == 'add') { $('#action_type_id').val(type); $('#p_id').val(id); } $.ajax({ type: 'post', url: 'addpost.php', data: $('#postform').serialize(), success:function(report){ $('#report').html(result); } }); } </script> </form> </body> </html>
now edit code data getting insert in database want show result in para in add.html have given id of para in success method not working, form validation not working.
please help. thank you.
you can use hidden value send data in server.in form place 2 fields.
<input type="hidden" name="action_type" id="action_type_id"/> <input type="hidden" name="id" id="p_id"/>
and ajax
method like:
function useraction(type,id){ var statusarr = {add:"added",edit:"updated",delete:"deleted"}; if (type == 'add') { $('#action_type_id').val(type); $('#p_id').val(id); } $.ajax({ type: 'post', url: 'addpost.php', data: $('#postform').serialize(), success:function(report){ alert(report); } }); }
explanation: before submitting form, set type
, id
value hidden field , send entire form server.now can able post data $_post['action_type']
, $_post['id']
.
Comments
Post a Comment