View page :
<label >Challan No.</label>
<input type="text" class="form-control" id="dispach_challan_no" name="dispach_challan_no" placeholder="Enter Challan No">
<script>
$.ajax({
url :"<?php echo base_url();>booking/dispatch_challan/DispatchChallanController/fetchId",
type:"GET",
dataType: "html",
success: function(data){
$('#dispach_challan_no').html(data);
},
error:function(data){
alert("error message"+data);
},async:false,
});
</script>
Modle page:
public function fetchId(){
$query= $this->db->select('*')->from('bilty')->get();
return $query->result_array();
}
Controller
public function fetchId()
{
$modelResult = $this->dispatchModel->fetchId();
}
Please use this --
View Page -
<label >Challan No.</label>
<input type="text" class="form-control" id="dispach_challan_no" name="dispach_challan_no" placeholder="Enter Challan No">
<script>
$.ajax({
url :"<?php echo base_url();>booking/dispatch_challan/DispatchChallanController/fetchId",
type:"GET",
dataType: "JSON",
success: function(data){
$('#dispach_challan_no').val(data[0]['id']);
},
error:function(data){
alert("error message"+data);
},async:false,
});
</script>
Model --
public function fetchId(){
$query= $this->db->select('*')->from('bilty')->get();
return $query->result_array();
}
Controller --
public function fetchId()
{
$modelResult = $this->dispatchModel->fetchId();
echo json_encode($modelResult);
}
Simply fetch ID from your response.
try like this
success: function(data){
var result = data;
$('#dispach_challan_no').html(result.id);
},
or
success: function(data){
var result = data;
$('#dispach_challan_no').html(result['id']);
},
Related
I have a record of carriers and clients. When selecting a carrier on the customer screen, I need it to be automatically filled out the carrier's e-mail and telephone. The PHP file is returning a JSON correctly, but it is dropping in the error of jquery and not in success. I checked in Firefox's Firebug and the HTML tab of the console, where the GET requisition is reset.
<?php
include "Config/config_sistema.php";
$res = mysql_query("SELECT * FROM transportadoras");
$menu_items = null;
while($ln = mysql_fetch_object($res)){
$menu_items[] = $ln;
}
json_encode($menu_items);
?>
<script>
$("body").on("change","#transportadoras",function(event){
event.preventDefault();
var trigger=$(this);
$.ajax ({
type: "POST",
url: "buscaDadosTransportadora.php",
dataType: 'json',
success: function (data) {
$("#telefone_transp").val(data.telefone);
$("#email_transp").val(data.email);
},
error: function (data) {
alert("Erro");
},
});
});
</script>
return in ajax
[{"ID":"5","Nome":"Vinicius","email":"viniciusbalbinot91#gmail.com","telefone":"32680018"},{"ID":"6","Nome":"teste","email":"teste#teste.com.br","telefone":"12345567"}]
You response is array .
[{"ID":"5","Nome":"Vinicius","email":"viniciusbalbinot91#gmail.com","telefone":"32680018"},....]
you should loop through data.
success: function (data) {
$.each(data,function(user){ /* code ...*/});
}
If you want some current row select one row from mysql .
hope it can help u
php.php
$inp=$_POST["data"];
$txt[0]=array("ID"=>"5","Nome"=>"Vinicius","email"=>"viniciusbalbinot91#gmail.com","telefone"=>"32680018");
$txt[1]=array("ID"=>"6","Nome"=>"teste","email"=>"teste#teste.com.br","telefone"=>"12345567");
for($c=0;$c<sizeof($txt);$c++)
{
if($txt[$c]["ID"]==$inp)
{echo json_encode($txt[$c]);}
}
index.php
ID:<input id="txt" name="txt" type="text" />
<br><br>
Name:<input id="name" name="txt0" type="text" disabled="disabled"/><br>
Email:<input id="email" name="txt0" type="text" disabled="disabled"/><br>
Tel:<input id="tel" name="txt0" type="text" disabled="disabled"/><br>
js
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("document").ready(function()
{
$("#txt").keyup(function ()
{
$.ajax(
{
type:"POST",
dataType:"json",
url:"php.php",
data:{data : $("#txt").val()},
success: function(data)
{
$("#name").val(data.Nome);
$("#email").val(data.email);
$("#tel").val(data.telefone);
},
error: function ()
{
alert("Error!");
}
});
});
});
</script>
I have done the code like this in view page and controller done in codeigniter
While pressing enter key multiple times data saving to the table. Can anybody help to solve this problem
View Page
<form>
<div class="cmt-box">
<textarea class="form-control" name="txtArea" id="txtArea<?php echo $row->id;?>" onkeypress="onTestChange(1)" rows="1"></textarea>
</div></form>
script
function onTestChange(id) {
$("#txtArea"+id).keypress(function(e) {
if(e.which == 13) {
dataString=document.getElementById("txtArea"+id).value;
$.ajax({
type: "POST",
url: "<?php echo site_url('show/insertcomment'); ?>",
data: { comment :dataString, id:id},
success: function(data){
location.reload();
}
});
}
});
}
$(".class_txtarea").keypress(function(e) {
if(e.which == 13) {
dataString=document.getElementById(this).value;
$.ajax({
type: "POST",
url: "<?php echo site_url('show/insertcomment'); ?>",
data: { comment :dataString, id:id},
success: function(data){
location.reload();
}
});
}
});
HTML:
<div class="cmt-box">
<textarea class="form-control mytext" name="txtArea" id="txtArea<?php echo $row->id;?>" rows="1"></textarea>
</div>
Jquery:
$(document).ready(function(){
$('.mytext').keyup(function (evt) {
evt = evt || window.event;
if (evt.keyCode == 13) { /* pressed enter key */
$.ajax({
type: "POST",
url: "<?php echo site_url('show/insertcomment'); ?>",
data: { comment :dataString, id:id},
success: function(data){
location.reload();
}
});
}
});
});
passing data to controller using AJAX
this is script i have written to pass data to controller but data is not passed to the controller
this is the input data i want to pass
<div class="form-group">
<table class="table table-striped b-t b-light text-sm">
<thead>
<tr>
<th>ID</th>
<th>Question</th>
<th>answer</th>
</tr>
</thead>
<tbody>
<?php foreach ($quet as $row) { ?>
<tr>
<td ><?php echo $row['id']; ?></td>
<td>
<?php echo $row['question']; ?>
</td>
<td><input type='text' name='name' required="required" class="form-control" placeholder='Enter Your Answer'></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<button class="btn btn-primary nextBtn btn-lg pull-right" id ="next" type="button" >Next</button>
and the script
<script>
$(document).ready(function($){
$("#next").click(function(){
var array = $("name").val()
$.ajax({
type: "POST",
datatype:"json",
url: BASE_URL+"/student/add",
data: 'data='+array,
contentType:'application/json',
processData: false,
error: function(response) {console.log('ERROR '+Object.keys(response)); },
success: function(response) {
console.log(response)
}});
return false;
});
});
</script>
and the student controller
function add(){
if($this->student_model->add($this->input->post()))
{
$response['success'] = TRUE;
}
else
{
$response['success'] = FALSE;
}
echo json_encode($response);
}
Try this. Your data may be in wrong format
data: {'data':array}
EDIT
<input type='text' name='answer' id='answer' required="required" class="form-control" placeholder='Enter Your Answer' />
<script>
$(document).ready(function($){
$("#next").click(function(){
var array = $("#answer").val() // see the change name to id, see the html also
$.ajax({
type: "POST",
url: BASE_URL+"/student/add",
data:{'data':array},
error: function(response) {console.log('ERROR '+Object.keys(response)); },
success: function(response) {
console.log(response)
}});
});
});
</script>
Also check the array is received properly in you JS
<script>
$(document).ready(function($){
$("#next").click(function(){
var array = $("#id").val()
$.ajax({
type: "POST",
datatype:"json",
url: BASE_URL+"/student/add",
data: {
'data':array
},
contentType:'application/json',
processData: false,
error: function(response) {console.log('ERROR '+Object.keys(response)); },
success: function(response) {
console.log(response)
}});
return false;
});
});
</script>
In your controller you should change $this->input->post() to$this->input->post('data')
From your mentioned code, you need to check these points :
1) As you are using contentType:'application/json', so use the data format as data: {'data':array}
2) Finally check whether the url url: BASE_URL+"/student/add", is accessible or not.
Hope this helps :)
You need to change following in your code.
In the Script
<script>
$(document).ready(function($){
$("#next").click(function(){
var array = $("input[name]").val();
$.ajax({
type: "POST",
url: BASE_URL+"/student/add",
data: {'data':array},
error: function(response) {console.log('ERROR '+Object.keys(response)); },
success: function(response) {
console.log(response)
},
datatype:"json"
});
return false;
});
});
</script>
In the Student Controller
function add(){
if($this->student_model->add($this->input->post('data')))
{
$response['success'] = TRUE;
}
else
{
$response['success'] = FALSE;
}
echo json_encode($response);
}
I hope this help.
What I have here is ajax that's post information into textbox from database. This ajax work's in input field, but when I tried to use select box it doesn't working. Why is that?
not working
<select id="tag"><option value="">none</option><option value="crs">crs</option></select>
working
<input name="tag" id="tag" type="text" value="" />
Index.php
<select id="tag"><option value="">none</option><option value="crs">crs</option></select>
<input name="output" id="output" type="text" value="" />
<script type="text/javascript">
$(document).ready(function()
{
$('input[id="tag"]').change(function()
{
var prjt_code = $("#tag").val();
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"prjt_code="+prjt_code,
dataType:'html',
type:'POST',
success:function(data){
//alert(data);
$('#output').val(data);
}
});
return false;
});
});
</script>
autocomplete-ajax.php
<?php
if(isset($_POST['prjt_code'])) {
$prjt_code = $_POST['prjt_code'];
$sql = $mysqli1->query("SELECT * FROM project WHERE project='$prjt_code'");
while($row1 = $sql->fetch_assoc())
{
$code = $row1['project_code'];
}
echo $code;
}
?>
You are targeting input[id="tag"] when you want select[id="tag"]
http://jsfiddle.net/releaf/U28jb/
$(document).ready(function() {
$('select[id="tag"]').on('change', function() {
var prjt_code = $("#tag").val();
alert(prjt_code);
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"prjt_code="+prjt_code,
dataType:'html',
type:'POST',
success:function(data){
//alert(data);
$('#output').val(data);
}
});
return false;
});
});
i have a website with a jquery search form. I have the searchboxes in every page of the site indide the header. Once you enter the searchwords there it redirects you the the search.php
What i want to do is to make the form submitted as soon as the page loads, because i send the data to the searchbox inside the search.php
Example: in page 1 i enter something and click submit. In search.php loads and in the main search field i have somethis(what i entered before). But i have to click again the submit to see the results. Any idea how can i make the form autosubmit when im being redirected? So i can see the results instantly.
Here's the code:
<form method="get" id="myForm" name="myForm" >
<input type="text" name="search" id="search_box" value="<?php $_GET['q']; ?>" placeholder="Enter band, artist, name..." autofocus/>
<input type="submit" value="Go" />
<br />
</form>
and the jquery:
<script type="text/javascript">
$(function() {
//-------------- Update Button-----------------
$(".search_button").click(function() {
var search_word = $("#search_box").val();
var dataString = 'search_word='+ search_word;
if(search_word==''){
} else {
$.ajax({
type: "GET",
url: "searchdata.php",
data: dataString,
cache: false,
beforeSend: function(html) {
document.getElementById("insert_search").innerHTML = '';
$("#flash").show();
$("#searchword").show();
$(".searchword").html(search_word);
$("#flash").html('<p style="text-align:center;">Loading Results...</p>');
},
success: function(html){
$("#insert_search").show();
$("#insert_search").append(html);
$("#flash").hide();
}
});
}
return false;
});
//---------------- Delete Button----------------
});
</script>
$(function(){
$('#myForm').trigger('submit');
});
When the page is ready you simply trigger the submit using jquery.
$(function() {
$(".search_button").click(function() {
var search_word = $("#search_box").val();
var dataString = 'search_word='+ search_word;
if(search_word==''){
} else {
$.ajax({
type: "GET",
url: "searchdata.php",
data: dataString,
cache: false,
beforeSend: function(html) {
document.getElementById("insert_search").innerHTML = '';
$("#flash").show();
$("#searchword").show();
$(".searchword").html(search_word);
$("#flash").html('<p style="text-align:center;">Loading Results...</p>');
},
success: function(html){
$("#insert_search").show();
$("#insert_search").append(html);
$("#flash").hide();
}
});
}
return false;
});
});
$(document).ready(function(){
$('#myForm').submit();
});
You can try this way.
<form method="get" id="myForm" name="myForm" >
<input type="text" name="search" id="search_box" value="" placeholder="Enter band, artist, name..." autofocus/>
<input id="button-submit" type="submit" value="Go" />
</form>
<script type="text/javascript">
$(function() {
function _myFunction () {
var search_word = $("#search_box").val();
var dataString = 'search_word='+ search_word;
if(search_word==''){
} else {
$.ajax({
type: "GET",
url: "searchdata.php",
data: dataString,
cache: false,
beforeSend: function(html) {
document.getElementById("insert_search").innerHTML = '';
$("#flash").show();
$("#searchword").show();
$(".searchword").html(search_word);
$("#flash").html('<p style="text-align:center;">Loading Results...</p>');
},
success: function(html){
$("#insert_search").show();
$("#insert_search").append(html);
$("#flash").hide();
}
});
}
return false;
}
$("#button-submit").click(function(e) {
e.preventDefault();
_myFunction();
return false;
});
_myFunction(); //This is where the magic happens
});
</script>
By doing that, the page will trigger on loading the function that is used when you click on the button.