I have this problem with assigning a certain array variable. When I post it to Codeigniter Controller, It produces this error Click to view Image.
View:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function submit() {
var TableData = {"table1":"sample1","table2":"sample2","table3":"sample3"};
var Data = JSON.stringify(myTableArray);
$.ajax({
type: "POST",
url: "http://janncomputing/tabulation/judges/save_Production",
data: {pTableData : Data},
success: function(result){ //retrieve data
alert('Success');
alert(result); //alert it
}//success
});
</script>
</head>
<body>
<div>
<button type="button" onClick="submit();">SUBMIT</button>
</div>
</body>
</html>
Controller:
public function save_Production()
{
$table = json_decode($_POST['pTableData'],true);
$msg = $table['table1'];
echo $msg;
}
try this
function submit()
{
var TableData = {"table1":"sample1","table2":"sample2","table3":"sample3"};
//var Data = JSON.stringify(TableData);
$.ajax({
type: "POST",
url: "http://janncomputing/tabulation/judges/save_Production",
dataType: 'json',
data: {pTableData : TableData},
success: function(result){ //retrieve data
alert('Success');
alert(result); //alert it
}//success
});
}
Related
i'm new to web and facing issue while using PHP file's constants inside javascript Ajax call.
My PHP code in constants.php file is as:
<?php
$color = 'green';
define ('BASE_URL', 'https://example.com?');
define ('APP_KEY', 'abcde');
define ('USER_KEY', '12345');
?>
My Ajax call in another login.php file is as:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#loginPopup").on('click',function(){
var x = document.forms["login"]["emailId"].value;
var pwd = document.forms["login"]["pwd"].value;
$.ajax({
type: 'GET',
url: 'BaseURL?appkey=abcde&userkey=12345&email='+ x +'&password=' + pwd,
crossDomain: true,
dataType: 'jsonp',
success: function (response) {
showAlert(response);
},
error: function (request, status, error) {
alert("ERROR");
}
});
});
});
</script>
I want to move constants i.e. Base URL, Keys etc to constants file. So i've created constants.php. But now, i don't know how to use that inside ajax call. Please help. Thanks in anticipation.
Like so, assuming the two files are in the same dir:
<?php
require_once('constants.php');
?>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#loginPopup").on('click',function(){
var x = document.forms["login"]["emailId"].value;
var pwd = document.forms["login"]["pwd"].value;
$.ajax({
type: 'GET',
url: '<?php echo BASE_URL ?>?appkey=<?php echo APP_KEY ?>&userkey=<?php echo USER_KEY ?>&email='+ x +'&password=' + pwd,
crossDomain: true,
dataType: 'jsonp',
success: function (response) {
showAlert(response);
},
error: function (request, status, error) {
alert("ERROR");
}
});
});
});
</script>
i have link in my page
+1111
i want to save every click in database
i made this ajax code :
<script type="text/javascript">
$('#tel').on('click', function() {
var location = $(this).attr('href');
var action = 'script.php';
$.ajax({
method: 'POST',
url: action,
data: '',
dataType: 'json',
success: function (data) {
window.location = location;
},
error: function(data){
return false;
}
});
});
</script>
now what must i do, what is the code i have to put in script.php
First you need send some data to script.php, for example
url: action,
data: {location: location},
dataType: 'json',
next in script.php you can read this data
$_POST['location']
create sql query and save data in DB.
send href as json
<script type="text/javascript">
$('#tel').on('click', function() {
var location = $(this).attr('href');
var action = 'script.php';
$.ajax({
method: 'POST',
url: action,
data: {"href":location}, // this line update
dataType: 'json',
success: function (data) {
console.log(data['state]); // this line add for debug server side
window.location = location;
},
error: function(data){
return false;
}
});
});
</script>
and update script.php like below:
<?php
if($_POST['href']){
//connect to database
//update table
echo json_encode(array('state'=>'ok'));
}else{
echo json_encode(array('state'=>'error'));
} ?>
I am trying to test AJAX in CodeIgniter, with no luck so far. Please tell me where I am going wrong.
Here is my test_page.php:
<!DOCTYPE html>
<head>
<script src="<?php echo base_url();?>assets/libs/jquery/jquery-2.2.3.min.js"></script>
<script src="<?php echo base_url();?>assets/libs/jquery/jquery-2.2.3.min.js"></script>
<script src="<?php echo base_url();?>assets/libs/js-cookie/js.cookie.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<button id="btn" onclick="gethtml()">ajax Test</button>
<script type="text/javascript">
function gethtml(){
var url = "<?php echo base_url();?>home/ajax_test";
alert(""+url);
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
success: function(data){
alert("ajax success");
}
});
}
</script>
</body>
</html>
Here is my function in controller:
public function ajax_test() {
echo "return from ajax";
}
This Type of AJAX Call dataType is not required
Remove
dataType: 'json'
Use like
<script type="text/javascript">
function gethtml(){
var url = "<?php echo base_url();?>home/ajax_test";
alert(""+url);
$.ajax({
url: url,
type: 'POST',
success: function(data){
alert("ajax success");
}
});
}
</script>
You can use below mentioned solution.
<button id="btn">ajax Test</button>
$(document).ready(function(){
$('#btn').Click(function(e){
e.preventDefault();
var url = "<?php echo base_url();?>home/ajax_test";
alert(""+url);
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
success: function(data){
alert("ajax success");
}
});
});
});
Let me know if it not works for you.
Error in the above code is the response from the ajax_test() is not encoded into json. So, the error event of json is triggered and the code inside success is not executed.
Always make sure to send json encoded data if you are specifying dataType as json.
Try on view
<button id="btn">ajax Test</button>
<script type="text/javascript">
$('#btn').on('click', function(e) {
// e.preventDefault(); Not sure if you need it.
$.ajax({
url:"<?php echo base_url('home/ajax_test');?>",
type: 'POST',
dataType: 'json',
// data: {
// someinput: $('#someinput').val()
//},
success: function(data){
alert(data['test']);
}
});
});
</script>
On controller you can use the output class also.
public function ajax_test() {
$json['test'] = "return from ajax";
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($json));
}
I am using the following script
<script>
$(document).ready(function(){
$("#view").click(function(){
var requestId = $("#hdnRequestId").val();
$.ajax({
type: "POST",
url: "enquiryProcess.php",
data: requestId,
cache: false,
success: function(data){
console.log(data);
}
});
return false;
});
});
My controller function is
<?php
include('enquiry_function.php');
$functionObj=new Enquiry();
if(isset($_POST['requestId']))
{
$qt_request_id=$_POST['requestId'];
$responce=$functionObj->view_enquiry_request($qt_request_id);
echo json_encode($responce);
}
?>
And my model function is
class Enquiry
{
public function view_enquiry_request($qt_request_id)
{
$query=mysql_query("SELECT * FROM quote_request WHERE qt_request_id='$qt_request_id'");
$result=mysql_fetch_assoc($query);
return $result;
}
}
I did not get any error.But result in console message is empty.How to get the result from php in jquery ajax.please help me.
Please change
var requestId = $("#hdnRequestId").val();
$.ajax({
type: "POST"
, url: "enquiryProcess.php"
, data: {"requestId":requestId}
, cache: false
, success: function (data) {
console.log(data);
}
});
Pass data as PlainObject or String or Array. See jQuery documentation here http://api.jquery.com/jquery.ajax/
i am new to JQuery AJAX and i need help with my code. What i want is when i click the add button it will change to delete button. but what happens in my code is that it changes to delete button but when i click delete button, it does not change back to add button. i want it to look like some sort of toggle. here's my html code with javascript:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#add_button').click(function(){
var temp = $('#add_button').val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {add_button: temp},
success: function(data){
$('div').html(data);
}
});
});
$('#delete_button').click(function(){
var temp = $('#delete_button').val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {delete_button: temp},
success: function(data){
$('div').html(data);
}
});
});
});
</script>
</head>
<body>
<div>
<button id="add_button" name="add" value="testing">Add</button>
</div>
</body>
and here's my php code:
<?php
if(isset($_POST["add_button"])){
echo "<button id='delete_button' name='delete' value='testing'>Delete</button>";
}
if(isset($_POST["delete_button"])){
echo "<button id='add_button' name='add' value='testing'>Add</button>";
}
?>
please help. thanks
You can try hiding the button clicked and show other button something like
$('#add_button').click(function(){
var temp = $('#add_button').val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {add_button: temp},
success: function(data){
$('div').html(data);
$('#add_button').hide();
$('#delete_button').show();
}
});
});
$('#delete_button').click(function(){
var temp = $('#delete_button').val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {delete_button: temp},
success: function(data){
$('div').html(data);
$('#delete_button').hide();
$('#add_button').show();
}
});
});
Or you can use jquery toggle
$( "#add_button").toggle()
http://api.jquery.com/toggle/
Your solution wasn't working, because delete button event handler was being attached to DOM before the DOM element(delete button) was initialised.
So, one event for both buttons would be enough:
$('button').click(function(){
var button = $(this);
if(button.data('type') == 'add') {
var postData = {add_button: button.val()};
} else {
var postData = {delete_button: button.val()};
}
$.ajax({
url: "ajax_test.php",
type: "POST",
data: postData,
success: function(data){
$('div').html(data);
}
});
});
HTML:
<div>
<button data-type="add" name="add" value="testing">Add</button>
</div>
PHP:
<?php
if(isset($_POST["add_button"])){
echo "<button data-type='delete' name='delete' value='testing'>Delete</button>";
}
if(isset($_POST["delete_button"])){
echo "<button data-type='add' name='add' value='testing'>Add</button>";
}
?>
<script>
$(document).ready(function () {
$('div').on('click', '#add_button', function(){
var temp = $(this).val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {add_button: temp},
success: function(data){
$('div').html(data);
}
});
});
$('div').on('click', '#delete_button', function(){
var temp = $(this).val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {delete_button: temp},
success: function(data){
$('div').html(data);
}
});
});
});
</script>
Your code won't work on dynamically added elements, while above code works.
Read http://api.jquery.com/on/ for more informations.
Try the below code:
Give a class name to button:
<button id="add_button" class="btnOp" name="add" value="testing">Add</button>
Then you can add a js click event to that classname and change its data accordingly:
$('.btnOp').on('click',function()
{
var text=$(this).text();
if(text==="Add")
{
var temp = $('#add_button').val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {add_button: temp},
success: function(data){
$('div').html(data);
$('.btnOp').val('testing');
}
});
}
else
{
var temp = $('#delete_button').val();
$.ajax({
url: "ajax_test.php",
type: "POST",
data: {delete_button: temp},
success: function(data){
$('div').html(data);
$('.btnOp').val('testing');
}
});
}
});
This will help you to change button values and functionality on success of ajax and acts like kind of toggle