I am working in WordPress.
I have below form 1 which is a search form and when I press search I get results with AJAX.
Now the result rows have a button or a link which, when clicked, should trigger another AJAX call and that should input the post values of the first form.
So there are two forms involved and second form takes input from the previous form and on clicking the link in the result of the first form search, a second ajax process should be triggered.
The second ajax does not work and it just refreshes the page.
Below is my html code for first search form and AJAX and other code of second form that is triggered on pressing the link from search result of first form.
First form
<form id="mydispimage" action="" method="post">
<select name="category" id="category" style="width:250px; background-color:lightgrey;">
<option value="" disabled="disabled" selected="selected" ">Select category</option>
<option value="Cutie Pie">Cutie Pie</option>
<option value="Chubby">Chubby</option>
<option value="Dimples">Dimples</option>
</select>
<input type="submit" id="displayimage" name="displayimage" value="Search" style="margin-left:15px; margin-bottom:15px;">
</form>
<div id="myresult" style="margin-bottom:15px; position:relative;"></div>
Second form, which is the result of ajax function of first form, is passed with JSON (it has a button in it called votes and when clicked I want another ajax function to be called and allow users to vote for that row).
When I press vote nothing is returned.
$results = $wpdb->get_results($wpdb->prepare($sql)) or die(mysql_error());
if (is_array($results) && count($results) > 0) {
$form = "";
foreach($results as $result) {
$form.= '<form id="voteform" action="" method="post">';
$form.= "<input name='category' type='hidden' value='$result->category'>";
$form.= "<img src='$result->path' width='150' height='150' >" . '<br /><br />';
$form.= "<input name='id' type='hidden' value='$result->uid'>";
$form.= "<input name='comp' type='hidden' value='$result->competition'>";
$form.= $result->username . '<br />';
$form.= $result->votessum . '<br />';
$form.= "<input style='margin-bottom:30px;' value='vote' name='submit' type='submit'/></form>";
} //end of foreach
$response['form'] = $form;
}
echo json_encode($response);
die();
Votes Function
// register & enqueue a javascript file called globals.js
wp_register_script( 'votess', get_stylesheet_directory_uri() . "/js/ajaxinsert.js", array( 'jquery' ) );
wp_enqueue_script( 'votess' );
// use wp_localize_script to pass PHP variables into javascript
wp_localize_script( 'votess', 'yes', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
function votes()
{
echo json_encode("pakistan zindabad");
die();
$ccc = $_POST['comp'];
$cat = $_POST['category'];
global $wpdb;
$compp = $wpdb->get_var("SELECT competition FROM competition ORDER BY cid DESC LIMIT 1");
$userid = $_POST['id'];
$myvote = 1;
if ($wpdb->insert('zvotes', array(
'zvotes' => $myvote,
'zcompetition' => $compp,
'aid' => $userid
)) == false) {
wp_die(json_encode('Database Insertion failed'));
die();
}
else {
echo json_encode('your vote was successfully recorded');
die();
}
}
Aucun commentaire:
Enregistrer un commentaire