html element id not getting after load it from ajax page - php

I am trying to load value of state field after change suburb dropdown. The suburb dropdown is coming from an ajax page after blur postcode text field. my jquery code is
$("#zip").blur(function(){
$.post("get_suburb_admin.php",{zip:$(this).val()}, function(d){
$("#suburb_cnt").html(d.suburb);
},'JSON');
});
$("#suburb").change(function(){
$.post("get_state.php",{suburb:$(this).val()}, function(a){
$("#state").val(a);
});
});
Html structure is
<tr>
<td>Suburb :</td>
<td id="suburb_cnt"><input type="text" name="suburb" id="suburb" value="<?php echo $row['suburb'];?>" /></td>
</tr>
<tr>
<td>Postcode :</td>
<td><input type="text" name="zip" id="zip" value="<?php echo $row['zip'];?>" /></td>
</tr>
<tr>
<td>State :</td>
<td id="state_cnt"><input type="text" name="state" id="state" value="<?php echo $row['state'];?>" /></td>
</tr>
But it is not responding after on change the suburb dropdown which is coming from ajax page. I am giving what is coming from ajax page here
$sql=mysql_query("SELECT town,region FROM au_postcode WHERE postcode='".$_REQUEST['zip']."'");
$arr=array();
$arr['suburb']='<select name="suburb" id="suburb">';
while($row=mysql_fetch_array($sql)){
$arr['suburb'].='<option value="'.$row['town'].'">'.$row['town'].', '.$row['region'].'</option>';
}
$arr['suburb'].='</select>';
echo json_encode($arr);
also suburb data is not getting after the form submit with method post.
$_REQUEST['suburb'] = nothing, after form submit
how can I solve it, pls help

try this
$("#suburb").change(function(){
var suburbVal = $(this).val();
$.post("get_state.php",{suburb:suburbVal}, function(a){
$("#state").val(a);
});
});
and same for #zip blur event

DOM prepares element on every page load/refresh but when you add thing dynamically to your page (without refreshing it) it does not re initiates DOM to go and detect that. So you have to notify javascript that find your element NOW. For which you can use/bind LIVE Method (for jQuery 1.7) or ON Method (for later versions of jQuery).
Here`s a simple example for CLICK event in jQuery.
$("#myButton").click(function(){
/*TO DO*/
});
and its another version (you can use LIVE/ON as per your jQuery`s Version)
$("#myButton").live("click", function(e){
/*TO DO*/
});
All the Best :)

I did not bind html element of suburb. You can do this by changing:
$("#suburb").change(function(){
$.post("get_state.php",{suburb:$(this).val()}, function(a){
$("#state").val(a);
});
});
to:
$("body").on("change","#suburb",(function(){
$.post("get_state.php",{suburb:$(this).val()}, function(a){
$("#state").val(a);
});
});

Related

Tagging system with dynamic input

Right now I'm trying to combine the bootstraps tagging-system with a dynamic input field code. I want for every dynamic generated field/div a tag-inputfield.
like this way:
Topic1: Title
Description: it's about a title
Tags: hello, boring, title
Topic2: it's a title again
Description: it's a description again
Tags: idk, help
(...) and so on.
My jquery file for adding additional fields looks like this:
$(document).ready(function () {
var maxGroup = 10;
$(".addMore2").click(function() {
$(".tagging").css("display", "none");
if ($('feld2').find('.fieldGroup').length < maxGroup) {
var fieldHTML = '<div class="form-group fieldGroup">' +
$(".fieldGroupCopy").html() + '</div>';
$('feld2').find('.fieldGroup:last').after(fieldHTML);
} else {
alert('Maximum ' + maxGroup + ' groups are allowed.');
}
});
//remove fields group
$("feld2").on("click", ".remove", function () {
$(this).parents(".fieldGroup").remove();
});
});
The not working part is this one:
When I click on my "add" button:
see here
it makes a copy of the this part:
<div class="form-group fieldGroupCopy" style="display: none;">
<table>
<tr>
<td class='first_td'><label for="titel"><b>Titel:</b></label></td>
<td><input type="text" name="description[]" class="form-control" placeholder="Title"/></td>
<td>-</td>
</tr>
<tr>
<td><b>Beschreibung:</b></td>
<td><textarea type="text" name="description[]" class="form-control" placeholder="Beschreibung des Themas"/></textarea></td>
</tr>
<tr>
<td><label for="Tags"><b>Tags</b></label></td>
<td colspan='2'>
div class="form-group">
<input type="text" name='tags_WiBe[]' placeholder='Add Tags' data-role="tagsinput" class="form-control" />
</div>
</td>
</tr>
</table>
</div>
But the tagging-system does not work within the copied fields.
Its only working outside of the div "form-group fieldGroup".
see here
Everytime I enter a tag the form wants to submit, but thats not what I want.
Please help me...
edit:
Here is a jsfiddle to show the problem more clearly.
https://jsfiddle.net/t5vrLsur/#&togetherjs=pbAhjTR1t1
I know it's not the most beautiful structure.
Don't be too hard on me. :(
The thing is that the plugin itself take care about the first initialisation on DOM ready. In your case, you want to run it after each adding card.
You can do this by simple run the plugin initialisation in the new card context.
Also, you have to remove the data-role attribute from the input in the template because, as we said, this attribute meant to initialise the plugin automatically but in the template's case we will do it manually after we will load the card.
So here are the relevant lines:
// wrap the html with jQuery so you could delete the inputtags wrapper later
var fieldHTML = $('<div class="form-group fieldGroup">' + $(".fieldGroupCopy").html() + '</div>');
$('feld2').find('.fieldGroup:last').after(fieldHTML);
// initialise again
fieldHTML.find('input').tagsinput();
Working demo
http://output.jsbin.com/dabijac/
Also, you have couple of jQuery references in your code which cause problem. You have either remove it or using use $.noConflict to keep them.

How to automatically get the Student Class by entering his Reg.ID in another textbox without a submit button

I have created a form in php/html for student attendance now what I want is that when I enter student reg.id in "std_id" text box his class in which he is studying should be fetched from the table of database and appear it in "class" text box.
The name of the table is ("student_attendance") in the database. I don't know how to do this. So please help me doing it.
`
Student Reg. ID:
<td> Class: </td>
<td> <input type="text" name="class"/> </td>
</tr>
<tr>
<td> Section: </td>
<td> <input type="text" name="section"/> </td>
<td> Session: </td>
<td> <input type="text" name="session"/> </td>
</tr>
<tr>
<td> Date: </td>
<td> <input type="date" name="date"/> </td>
</tr>
<tr>
<td> Present Status: </td>
<td> <input type="text" name="pstatus"/> </td>
</tr>
</table>
<input type="submit" name="SAVE"/> //is used to save the form data in db
</form>`
Thanks
You can use following code :
$(document).ready(function (){
$("input[name=std_id]").blur(function (){
var reg_id=$("input[name=std_id']").val();
// now make ajax call for fething class data from table :
$.ajax({
type:'get',
url: 'test.php',
data:{regid:reg_id}
}).done(function(result){
//store response in class textbox:
$("input[name=class']").val(result);
});
});
Dont forget add jquery and ajax plugin.
You can use OnChange() attribute in javascript to trigger your database from background.
Or you can use Ajax.
Simply you need to do an ajax call to get data from backend.
Ajax is a technology to perform requests from browser without refreshing the page.
It does a request and gets response (usually returned in json format) based on which you decide in frond end what to do (maybe display your desired data from response somewhere).
Here is a tutorial on general ajax: http://www.w3schools.com/ajax/
Also it is nice to attach jQuery lib and use it's $.ajax() method with its success and fail handlers: http://api.jquery.com/jquery.ajax/
You should have a php file where you will get the class by student id. Something like this:
<?php
$id = $_GET('std_id');
//connect to mysql
//select * from student_attendance where std_id = $id;
//get result
echo array('class'=>$result['class']);
?>
And in frontend on reg.id blur or change:
$.ajax({
url: "test.php",
type: 'POST'
}).done(function(response) {
$('input[name=class]').val(response.class);
});

how do i post textbox value which is dynamically created by jquery

i am working with codeigniter framework i am trying to use textboxes in HTML form when the user click the button a textbox will automatically generated by jquery but when i submit the form the other textbox values are posting correctly but i can't post the jquery textbox value.
here is the jquery code:
$("#button").live('click',function add(){
$("#tblRel").append(
'<tr ><td>'
+'<input type="text" class="input_value_short" id="prova" name="jquery_txt" value="prova" />'+
'</td><tr>'
);
});
HTML:
<?php echo form_open("validation/form_main");?>
<table id="tblRel" border="0">
<tr>
<td class="lable">Applicant Name:</td>
<td colspan="3"><?php echo form_input(array('name'=>'applicant_name','class'=>'input_value_long','value'=>set_value('applicant_name'))) ?></td>
</tr>
</table>
<?php echo form_submit(array('id'=>'submit','name'=>'Save','value'=>'Save record','class'=>'button_print'));?>
in the form_main() function i am posting like this:
form_main(){
$name = $this->input->post('applicant_name');
$jquery_txt = $this->input->post('jquery_txt');
}
i can get the value of $name but the $jquery_txt is empty
can anyone help me please! and sorry form my poor english.
$("#button").live('click',function add(){
$("#tblRel").append('<tr ><td><input type="text" class="input_value_short" id="jquery_txt" name="jquery_txt[]" value="prova" /></td><tr>');
});
I guess your name and is are different for the textbox..Make it same and if you have more than one textbox change the name of the textbox into array otherwise you will get only one value after posting it.
Change the jQuery code to this, and it should work fine. Your problem was that you were adding the contents with the "submit" button's click, which would submit the form and the contents would get appended after the submit request.
<script>
$("form").live('submit',function add(){
$("#tblRel").append(
'<tr ><td>'
+'<input type="text" class="input_value_short" id="prova" name="jquery_txt" value="prova" />'+
'</td><tr>'
);
return true;
});
</script>

jquery data attribute with input selector in a loop

I have an html table built from a database query which loops through and creates a button for each camera name found and puts them in a table:
<?php
for($i=0;$i<$num_rows;$i++)
{
?>
<tr>
<td>
<input type="submit" class="play" data-hash="<?php echo $result_cameras[$i]["camera_hash"]; ?>" value="<?php echo $result_cameras[$i]["camera_name"]; ?>">
</td>
</tr>
<?php
}
?>
This resolves to something like this:
<tr>
<td>
<input type="submit" class="play" data-hash="0d3d0ac6e54a640c73f1149d4d0bbc38e99d10f5" value="Office Window">
</td>
</tr>
<tr>
<td>
<input type="submit" class="play" data-hash="b824cba374c3d5ab7806ad8260c939323c03147b" value="aaa">
</td>
</tr>
<tr>
<td>
<input type="submit" class="play" data-hash="ec9658f0c1855e2e2ac09ae284f5e6990dbf445d" value="laptop">
</td>
</tr>
Notice the data hash attribute is different for each button. I want to process this button with my jquery code:
$(".play").click(function(){
var camerahash = $('input').data('hash');
console.log($('input').data('hash'));
});
No matter which button I click I will always get the hash from the first button I click: 0d3d0ac6e54a640c73f1149d4d0bbc38e99d10f5. Any help is appreciated.
$('input').data('hash') gives you the data attribute of the first input in the selection use $(this).data('hash') to get the data attribute of the currently clicked input
You need to specify which input element to read.
Try something like:
$(".play").click(function(){
$this = $(this);
var camerahash = $this.data('hash');
console.log($this.data('hash'));
});
You are always calling the first object of .play. This would be a correct way:
$('.play').on('click', function(){
var camerahash = $(this).data('hash');
});
You could always grab them by using the .attr(data-hash) html5 attribute.
Try:
$('.play').on('click', function() {
var _hash = $(this).attr('data-hash');
console.log(_hash);
});
Your selector will return the first one that it comes to. Use this instead
<script>
$("input.play").click(function() {
alert($(this).data('hash'));
});
</script>

Wordpress jQuery to PHP data transfer

everyone. I'm a relatively new developer with limited experience in jQuery AJAX and PHP. I'm working on a Wordpress plugin that is very much a learning exercise for me as well.
Basic description: On my plugin's admin page, I have a bunch of forms that will be displayed as modal windows (using jQuery UI) and when filled out, will submit their fields to a separate PHP file for processing. That file will accept the data and prepare it for insertion into the wpdb table I have set up.
Plugin Admin page (PHP):
<div id="form1" title="My Awesome Form">
<form id="frmNewCom">
<fieldset>
<table>
<tr>
<td><label for="name">Community Name</label>
<td><input type="text" name="newComName" id="newComName" />
</tr>
<tr>
<td><label for="lefthead">Left Column Header</label></td>
<td><input type="text" name="newComLefthead" id="newComLefthead" /></td>
</tr>
<tr>
<td><label for="righthead">Right Column Header</label></td>
<td><input type="text" name="newComRighthead" id="newComRighthead" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
jQuery UI code for form ($pcal is my no-conflict thing):
$pcal('#form1').dialog({
autoOpen: false,
height: 275,
width: 400,
modal: true,
buttons: {
"Add Living Option": function() {
// Functionality for submit
},
Cancel: function() {
$pcal(this).dialog("close");
}
},
close: function() {
// close function
}
});
Now here's the problem. I'm not really sure what to do from this point. I've read a bunch of stuff about using .post() and .serialize(), and I've gotten myself very confused at this point.
Do you guys have some insight into the proper javascript/jQuery to PHP handling you could lend? I've asked this on the Wordpress forum as well, but I've always found good advice here, so thought I'd ask. Any and all help is appreciated.
A very simple example would be :
$pcal.post("test.php", $pcal("#frmNewCom").serialize(), function() {
// this will run once returned from PHP
alert('thanks');
$pcal(this).dialog("close");
});
This posts the form (id = frmNewCom) in a serialized format (newComName=1&newComLefthead=2&newComName=3 where 1,2 and 3 are the values entered in your form) using .post() and .serialize()
then in test.php you would access your values like this :
$newComName = $_POST['newComName'];
$newComLefthead = $_POST['newComLefthead'];
$newComName = $_POST['newComName'];
// insert into table
The above is a) untested and b) does not contain any prevention against SQL injection should you want to store this in a DB

Categories