Database data in jquery dialog - php

Ok, i tried it with jquery ajax to achieve the same. But now the system always shows the first id. It doesn't matter at which edit_subscriber image i click. This is what i did:
Jquery code:
jQuery(".edit_subscriber").click( function() {
jQuery('.dialog-modal').dialog({
modal: true,
open: function ()
{
jQuery.ajax({
async: false,
type: 'get',
url: nieuwsbrief.editsubscriber,
data: 'ajax=&subscriber_id=' + jQuery('.edit_subscriber').prop('id'),
success: function(data) {
jQuery('.dialog-modal').html(data);
}
});
},
close: function ()
{
},
height: 400,
width: 600,
title: 'Ajax Page'
});
return false;
});
html code:
echo '<div class="dialog-modal" title="Basic modal dialog" style="display: none;"></div>';
edit_subcriber.php:
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/www/wordpress/wp-config.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/www/wordpress/wp-load.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/www/wordpress/wp-includes/wp-db.php';
global $wpdb;
echo $_GET['subscriber_id'];
?>

The issue with the script is, you are using same id for all dialog-models, and when you are clicking on the edit_subscriber, it is picking the dialog form the id, and as you are picking form id, it will always get the first element with the id.
Multiple element with same id is not valid.
So you can try this.
Just replace this code
echo '<div id="dialog-modal" title="Basic modal dialog" style="display: none;">
Naam: <input type = "text" name = "subscriber_name" id = "'.$abonnee->subscriber_id.'" value = "'.$abonnee->subscriber_name.'"> <br />
</div>';
with this
echo '<div class="dialog-modal" title="Basic modal dialog" style="display: none;">
Naam: <input type = "text" name = "subscriber_name" id = "'.$abonnee->subscriber_id.'" value = "'.$abonnee->subscriber_name.'"> <br />
</div>';
and change js function to :
jQuery(".edit_subscriber").click( function() {
$(this).closest('tr').next().dialog({
modal: true,
open: function (){},
close: function (){},
height: 400,
width: 600,
title: 'Ajax Page'
});
});
As you are placing div after tr which is also not valid markup.

Related

success message in popup div without refreshing page

I want to display text message for success in popup div without refreshing the page
My popup form like this
<div class="modal-content">
<div id="message">Your message has been sent.<br /><br /></div>
<form action="<?php echo $_SERVER['REQUEST_URI'];?>" id="CallBackForm" name="CallBackForm" method="post">
<div class="form-group">
<input id="custmobileNo" name="custmobileNo" type="text" required="required">
<input type="submit" value="call" id="callback" name="callback" class="btn btn-info">
</div>
</form>
</div>
When I click on this link pop up will call
<a href="#" data-toggle="modal" data-target="#call-back">
<input type="submit" value="Call" class="btn-d btn-doctor" >
</a>
CSS:
<style type="text/css">
#message {
display:none;
font-size:15px;
font-weight:bold;
color:#333333;
}
</style>
Ajax Call:
<script>
$("#callback").click(function () {
var custmobileNo = $("#custmobileNo").val();
$.ajax({
url: "<?php echo base_url('Call'); ?>",
type: 'post',
data: {custmobileNo: custmobileNo},
beforeSend: function () {
if (custmobileNo != "") {
$("#message").fadeIn(); //show confirmation message
$("#CallBackForm")[0].reset(); //reset fields
}
}
});
});
</script>
I wrote beforeSend function() with some data either it was standard code or not I don't know. It was calling message fine but it was not closing I want it will close with in some seconds and click on again that button success message validation was showing I want clear that text also.
Try something like this maybe :
$.ajax({
url: "<?php echo base_url('Call'); ?>",
type: 'post',
data: {custmobileNo: custmobileNo},
beforeSend: function () {
if (custmobileNo != "") {
$("#message").fadeIn(); //show confirmation message
$("#CallBackForm")[0].reset(); //reset fields
}
},
success: function() {
setTimeout(function() { //hide message after a certain time
$("#message").fadeout();
}, 5000); // choose after how many time message should hide, here 5s
}
});
But if your Ajax call fail you won't go in the success part so becareful !
You can do the same with complete : function() {...} instead of success : function() {...}, here is some information about Ajax event : https://api.jquery.com/Ajax_Events/
Is it what you are looking for?

how to checked the checkbox after fetching the data from database using jquery and php

when user click on a button it displays the dynamic data fetched from the database along with the appropriate check box ..so user has select or check the checkbox of some data and submit the result then those data are stored in database.
again that user clicked the same button then again those data are displayed but this time the data which are selected buy the user previously should be checked. I am trying to do that but enable to write code for it in jquery.
below is my code...
javascript
$(document).ready( function() {
$('#location_choose').click(function(e){
branch();
document.getElementById('brn_light').style.display='block';
document.getElementById('fade').style.display='block';
});
function branch()
{
//alert(user_id);
$.ajax({
url: "<?php echo base_url(); ?>/login/branch",
data: {},
type: "post",
cache: false,
success: function (data)
{
//alert(data);
var obj = $.parseJSON(data);
var result = "";
<?php foreach($resultsb as $rows){?>
for ( var i = 0; i < obj.length; i++ )
{
result+="<table id='branchtable'><tr height='25px' ><td>&nbsp&nbsp</td><td ><input class='test' type='checkbox' name='branch_name' value="+obj[i].id+"<?php echo ($rows['branch_id']=="+obj[i].id+"? 'checked' : '');?>></td><td width='15px'></td><td width='630px'><b>"+obj[i].branch_name+
"</b></td></tr><tr><table id='branch_address' style='background-color:#EBF5FB'><tr><td>&nbsp&nbsp</td></tr><tr><td width='60px'></td><td width='440px'>"+obj[i].address+"</td><td width='40px'></td></tr><td>&nbsp</td></table></tr><tr></tr></table>";
}
<?php }?>
//alert(result);
document.getElementById("branch_table").innerHTML=result;
},
error: function (xhr, ajaxOptions, thrownError)
{
//alert(thrownError);
}
});
}
});
i tried using
<?php echo ($rows['branch_id']=="+obj[i].id+"? 'checked' : '');?>
in above code but still it doesnt checked the previous selected one.
Give The Same Class of Checkbox and Write Jquery Code
<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script>
$( document ).ready(function() {
$( '.select_all_text').click(function() {
var is_check = $(".select_type").prop('checked');
if(is_check == false)
{
$(".select_type").prop('checked', true);
$(''.select_all_text").text('De-select All');
}
else
{
$(".select_type").prop('checked', false);
$('.select_all_text").text('Select All');
}
});
});
</script>
</head>
<body>
<p style="position: sticky; z-index: 100; width: 50%;"><a id="select_all" onclick="check_uncheck" style="margin-right: 2%;" href="javascript:void(0)">Select all</a>
<?php
foreach($data_list as $data)
{
?>
<input type="checkbox" name="select_type[]" value="<?php echo $item_id; ?>" class="select_type" id="select_type"/>
<?php
}
?>
</body>
</html>

jQueryUI Dialog with ajax-injected html selector

All code below is a stand-alone working example (greatly simplified) of what I am trying to do. If anyone copy/pastes the below code blocks into 3 separate files, the code is fully self-contained-- just remember to reference/include test5.js and the jquery libraries in script tags at top of document.
SUMMARY: HTML div injected via Ajax not opening in the jQuery UI dialog widget.
Objective: On document load, jquery-ajax injects an html form (ultimately, it will retrieve appropriate form values from DB which is the reason for ajax). A div with id="clickme" is injected with the html. Clicking the div should open the dialog.
Problem: The jQueryUI .dialog does not appear. I put an alert box inside the click event, and that fires. But the dialog remains elusive.
Therefore, problem appears to be the fact that the HTML is injected. What am I missing?
HTML: index.php
<div id="putit_here">
</div>
JAVASCRIPT/JQUERY: test5.js
$(function() {
var pih = $('#putit_here');
$.ajax({
type: "POST",
url: "ajax/ax_test5.php",
data: 'contact_id=1',
success:function(data){
pih.html(data);
var etc1 = $( "#editThisContact_1" );
/* *****************************************************************
Moving Dialog up >here< was correct answer.
********************************************************************
etc1.dialog({
autoOpen: false,
height: 400,
width: 600,
modal: true,
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
alert('DialogClose fired');
}
}); //end .Dialog
****************************************************************** */
}
}); //End ajax
/* **** This is where I had it previously ***** */
etc1.dialog({
autoOpen: false,
height: 400,
width: 600,
modal: true,
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
alert('DialogClose fired');
}
}); //end .Dialog
$(document).on('click', '#clickme', function(event) {
alert('HereIAm...');
$( "#editThisContact_1" ).dialog( "open" );
}); //End #clickme.click
}); //End document.ready
AJAX - ax_test5.php
$rrow = array();
$rrow['contact_id'] = 1;
$rrow['first_name'] = 'Peter';
$rrow['last_name'] = 'Rabbit';
$rrow['email1'] = 'peter.rabbit#thewarren.nimh.com';
$rrow['cell_phone'] = '+1.250.555.1212';
$r = '
<div id="editThisContact_'.$rrow['contact_id'].'" style="display:none">
<p class="instructions">Edit contact information for <span class="editname"></span>.</p>
<form name="editForm" onsubmit="return false;">
<fieldset>
<span style="position:relative;left:-95px;">First Name:</span><span style="position:relative;left:10px;">Last Name:</span><br />
<input type="text" id="fn_'.$rrow['contact_id'].'" value="'.$rrow['first_name'].'" name="fn_'.$rrow['contact_id'].'">
<input type="text" id="ln_'.$rrow['contact_id'].'" value="'.$rrow['last_name'].'" name="ln_'.$rrow['contact_id'].'"><br /><br />
<span style="position:relative;left:-120px;">Email:</span><span style="position:relative;left:30px;">Cell Phone:</span><br />
<input type="text" id="em_'.$rrow['contact_id'].'" value="'.$rrow['email1'].'" name="em_'.$rrow['contact_id'].'">
<input type="text" id="cp_'.$rrow['contact_id'].'" value="'.$rrow['cell_phone'].'" name="cp_'.$rrow['contact_id'].'">
</fieldset>
</form>
</div>
';
echo $r;
EDIT:
Updated question to move dialog definition inside AJAX success callback. Did not completely solve problem, though. The dialog now appears if I change the autoOpen flag to true, but that is not how the script must work. The dialog still does not open upon clicking the (injected) #clickme div.
EDIT 2:
My bad. At first I thought it didn't work, but then found that my live test and posted SO question varied in one line: how the .dialog("open") was being called. In live code, it was still using the var: etc1.dialog("open") -- but in post above the selector was fully referenced: $('#editThisContact_1').dialog("open"). The posted syntax was correct. Thanks gents, and also itachi who got me to check chrome console.
You are trying to initialize a dialog on an element before the element exists. You need to initialize the dialog on "#editThisContact_1" after your ajax call comes back successfully.
Like this:
....
success:function(data){
pih.html(data);
//now your DIV is actually there so init the dialog
var etc1 = $( "#editThisContact_1" );
etc1.dialog({
autoOpen: false,
height: 400,
width: 600,
modal: true,
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
alert('DialogClose fired');
}
}); //end .Dialog
}

JQuery select in dialog does not have options appended

I am trying to use a JQuery dialog to act similar to an open file dialog, but instead of files I am using records from a database to populate the dialog.
The problem that I am having is that I cannot seem to get the html option data loaded into the html select in my dialog. I have used the Google Javascript debugger in Chrome and have stepped through my javascript code which is called from my PHP ajax. I see no errors and I see the data is there and formatted correctly when I put it into the append statement. I don't understand why it does not show up in my html select.
Thanks for your help.
below is my Ajax function which receives the data.
function doOpenAjax(filterStr)
{
$.get(
"contenteditServerAjax.php",
{filter: filterStr },
function(data) {
var optsArr = data.split('|~|');
var seloption = "";
$.each(optsArr, function(i) {
seloption += '<option value="' + optsArr[i] + '">' + optsArr[i] + '</option>';
});
$('#chooseArticleName2').find('option').remove().end();
$('#chooseArticleName2').append(seloption);
},
"html"
);
}
For reference this is part of my php which contains the jquery dialog:
<head>
<script>
$(document).ready(function() {
var dlg = $("#opendiv").dialog({modal:true, height:550, width:650,
hide:{ effect: 'drop', direction: "down" },
autoOpen:false,
maxHeight: 1200,
maxWidth: 1200,
minHeight: 250,
minWidth: 300,
buttons: { "Open": function() { $('#state').val('Open Doc'); $('#contentform').submit(); $(this).dialog("hide"); },
"Cancel": function() { $(this).dialog("close");}}
});
dlg.parent().appendTo($("#contentform"));
});
</script>
<div id="opendiv" name="opendiv" title="OPEN">
<p align="center">Enter Name to Open Article as:</p>
<p>
Filter:
<input name="docFilter2" id="docFilter2" type="text" value="$filter2" maxlen="64" size="28"/>
<input name="LoadTitles2" id="LoadTitles2" type="button" value="Load Available Titles"
onclick="doOpenAjax( $('#docFilter2').val() );"/>
</p>
<p>
<div id="ajaxOutput2">
<select name="chooseArticleName2" id="list2" size="8" style="min-width:92%"
onchange="
var mytext = $('#chooseArticleName2 :selected').text();
$('#textBoxArticleName2').val(mytext);">
</select>
</div>
</p>
<p>
Document:
<input type="text" id="textBoxArticleName2" name="textBoxArticleName2" size="56"/>
</p>
</div>
Just FYI, it would be more efficient to do this:
$('#chooseArticleName2').empty().html(seloption);
Than this:
$('#chooseArticleName2').find('option').remove().end();
$('#chooseArticleName2').append(seloption);

Form submit stopping JSON objects from displaying

I'm building a form with a single field and a submit button. A user enters a value, clicks the button and the value is sent to a .ajax method which sends a POST request to the Google Shopping API.
At the moment, whenever I try and run the method once the submit button is clicked the JSON response isn't being received. As soon as I remove the code on button click, it reappears.
Any help would be great.
<!DOCTYPE html>
<html>
<head>
<style>
#images { padding:0; margin:0; overflow: hidden;}
#images img { width:200px; height:200px; border:none;}
#lists li { display: table;}
#lists img { width:200px; height: 200px; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<h1 id="title"></h1>
<p id="description"></p>
<div id="images"></div>
<div id="lists"></div>
<form id="myform">
<input type="text" name="myanswer" value="test">
<input type='submit' class="button" name="submitButton" value='submit'>
</form>
<script>
var apiKey = "key-removed-from-stackoverflow";
var country = "US";
var apiurl = "https://www.googleapis.com/shopping/search/v1/public/products?callback=?";
$(document).ready(function() {
$('.button').live('click', function() {
$.ajax({
type: "POST",
url: apiurl,
dataType: 'jsonp',
data :
{
key: apiKey,
country: country,
q: "star"
},
success: function(data) {
$.each(data.items, function(i, item){
if (item.product.images.length > 0) // sanity check
{
//global variables
var link = item.product.images[0]['link'];
var title = item.product.title;
var listData = "<li>" + title + "</li>" + '<img title="' + title + '" src="' + link + '" />';
$('#lists').append(listData);
var img = $("<img/>").attr("src", link);
$("<a/>").attr({href: link, title: "Courtesy of James"}).append(img).appendTo("#images");
console.log(data)
}
});
// console.log(data);
console.log(data)
}
});
});
});
</script>
</body>
</html>
add e.preventDefault() to your handler. Also remember to pass in the event parameter into the function too...
$('.button').live('click', function (e) {
e.preventDefault();
//rest of your code
});
Also, there is no reason to use .live() in this situation. I'd just bind it directly to the element -
$('.button').click(function () {
});

Categories