I'm working on joomla environment. I would like to run small php script by click on button event from JQuery reason I would like to use JQuery is I cannot modified existing component. Current component adding some values on Database from webpage. I would like to do same thing but from back-end with out notify any thing from another PHP file. I was trying something as code but it's not working.
Is there any other way.?
What is wrong in code.?
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#submitButton').click(function(){
$.post("/automate/UpdateMySql.php");
});
}
</script>
You were missing a dot and bracket(with semi-colon) at the end. As per the comments if you want to just Update Date And Time. You can post any random variable to UpdateMySql.php and then Create Date And Time there.
<script type="text/javascript">
var clicked = 0;
jQuery(document).ready(function(){
jQuery('#submitButton').click(function(){
clicked = 1;
jQuery.post("/automate/UpdateMySql.php" , { click : clicked });
});
});
</script>
In UpdateMySql.php write.
$clicked = $_POST['click'];
if(isset($clicked) && !empty($clicked)) {
$today = date("Y-m-d H:i:s");
$query = "UPDATE table_name date_time=".$today." WHERE anything = 'anything'";
if($query) {
return 'Updated';
} else {
return 'Something Went Wrong';
}
}
Please use:
$(document).ready(function() {
$('#submitButton').click(function(e) {
$.ajax({ type:'POST',
url:'/automate/UpdateMySql.php'
data:{name:'demo',id:"1"},
sucess:function(xhr) {
alert('table updated');
}
});
});
});
if some one need complete answer in future.
<script type="text/javascript">
var clicked = 0;
jQuery(document).ready(function(){
jQuery('#submitButton').click(function(){
clicked = 1;
jQuery.post("/automate/UpdateMySql.php" , { click : clicked });
});
});
</script>
Related
I'm trying to pass a javascript variable to php on the same page. I tried some code but nothing worked.
The current code looks like this:
function init(e){
$('#DeleteDaily').click(function() {
d = document.getElementById("DailyRequestsList");
selected = d.selectedIndex;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "index.php?i=" + selected, true);
xhttp.send();
});
}
$(document).ready(init);
$(document).ready(function() {
$('#myForm').ajaxForm(function() {
<?php
if(isset($_POST['DeleteDaily'])) {
$Index = $_GET['i'];
}
?>
});
});
If I try to use Index as an argument for a python script it should delete an entry in a textfile and an element from a select object should be deleted on the website which doesn't happen. The python script itself works fine.
But I don't know why the variable isn't passed to php.
You can't just "mix" javascript and PHP like you're doing.
If that is inside a web page, the PHP code will be rendered (in your case, it will return nothing), and the page will just interpret an empty javascript function.
You need that PHP to be on the server, or turn it into javascript...
you can done it by
PHP Code
<?php
if(isset($_POST['DeleteDaily'])) {
$Index = $_GET['i'];
}else{
$Index='';
}
?>
Javascript code
$(document).ready(function() {
$('#myForm').ajaxForm(function() {
var index = '<?php echo $Index;?>';
if(index == ''){
//put your code
}else{
//put your code
}
});
});
Following are two code blocks, each one is responsible for AJAX call to a PHP file onclick of respective hyperlink:
<script language="javascript" type="text/javascript">
$(".fixed").click(function(e) {
var action_url1 = $(this).attr('delhref');
var qid = $(this).data('q_id');
$(".fixed").colorbox({inline:true, width:666});
$("#fixedPop_url").click(function(event) {
event.preventDefault();
$.get(action_url1, function(data) {
alert("Question status updated successfully");
$("#fix_"+qid).hide();
$("#notfix_"+qid).show();
});
});
$(".c-btn").bind('click', function(){
$.colorbox.close();
});
});
$(".notfixed").click(function(e) {
var action_url2 = $(this).attr('delhref');
var qid = $(this).data('q_id');
$(".notfixed").colorbox({inline:true, width:666});
$("#notfixedPop_url").click(function(event) {
event.preventDefault();
$.get(action_url2, function(data) {
alert("Question status updated successfully");
$("#notfix_"+qid).hide();
$("#fix_"+qid).show();
});
});
$(".c-btn").bind('click', function(){
$.colorbox.close();
});
});
</script>
Now the PHP code snippet from a file is written below to which the AJAX request is made. Actually, the PHP file name to which the AJAX request is going and the parameters passed required are contained in variables action_url1 and action_url1. These are working fine. Until now there is no issue for me. Also the PHP code is working fine.
<?php
$objQuestionIssue = new QuestionIssue;
$op = $_GET['op'];
switch( $op ) {
case "fixed":
$que_issue_data = $objQuestionIssue->UpdateQuestionIssueStatus($question_id, $op);
die();
break;
case "notfixed":
$que_issue_data = $objQuestionIssue->UpdateQuestionIssueStatus($question_id, $op);
die();
break;
}
?>
But the issue I'm facing is getting the alert success message multiple times. It is expected to show the alert message only once but in current scenario it's showing multiple times. Can anyone hep me in correcting this issue please?
You have nested click handlers -
Every time you click on .fixed you are binding a new click handler on #fixedPop_url. And that is why your .get is executing multiple times
you can use .off() to fix this -
$("#fixedPop_url").off('click').on('click',function(event) {
I have a form where users can save activity of the day, with date and time.(index.php). I have build a jquery that opens a dialog popup window on page load...I want to include this code in my second php page. This page is called add.php. the user go there when he press a submit button in page index.php. When he press the submit button, to add an activity and exist an activity in that time and date I want to show a popup. But how can I include the popup below in the add.php.
My code is below
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("#dialog").dialog({
title: "jQuery Dialog Popup",
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
});
</script>
<div id="dialog" style="display: none">
You have an activity on this time
</div>
And this is add.php
<?php
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
die('<div class="content">Lidhja me databazen nuk mund te kryhet</div>' .mysql_error(). ' </body></html>');
}
if(!mysql_select_db("Axhenda",$con))
die('<div class="content">Nuk mund te hapet databaza Axhenda</div>'.mysql_error(). '</body></html>');
$Data=$_POST['date'];
$Ora=$_POST['time'];
$Emri=$_POST['emritakimit'];
$Pershkrimi=$_POST['pershkrimi'];
session_start();
if (!isset($_SESSION['user_id'])){
header('location:index.php');
}
//variabli SESSION per te ruajtur ID e perdoruesit
$perdoruesi=$_SESSION['user_id'];
$selekto=mysql_query("SELECT * FROM aktiviteti WHERE Data='$Data' and Ora='$Ora'");
$nr_rreshtave=mysql_num_rows($selekto);
if ($nr_rreshtave>0)
{
//here I want to include the function above
header('locationindex.php');}
else
{
$query ="INSERT into aktiviteti VALUES('', '$perdoruesi', '$Emri', '$Pershkrimi' ,'$Data','$Ora')";
$result=mysql_query($query,$con);
if($result)
{ header('location:index.php?error-akt1=1');}
else
{ header('location:index.php?error-akt2=1');}}
mysql_close($con);
?>
please help me...Thanks in advance
You actually want the pop up to show up only if there is an error, in which case add.php redirects to an URL with error-akt1=1 or error-akt2=2 as parameter. If you added the pop up to add.php, it would break the redirects. Therefore, you need to do it on index.php instead, but ONLY if the parameters are loaded.
To make the parameters available, to you, use the code provided by BrunoLM in this post: How can I get query string values in JavaScript?
(or any other code in that post, but BrunoLM provided one in jQuery, instead of pure Javascript). I'm copying the main part here:
(function($) {
$.QueryString = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&')) })(jQuery);
Get the querystrying by putting:
$.QueryString["param-name"]
And once you have the parameter, just use an if statement around the loading of your alert, like so:
$(function () {
var error-akt1 = $.QueryString["error-akt1"];
if (error-akt1 == 1) {
$("#dialog").dialog({
title: "jQuery Dialog Popup",
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
}
});
Repeat for error-akt2 or, even better, use error-akt=1 or error-akt=2 to send different erro r messages in the same parameter and querystring variable.
This is the script which will send information of textboxes to another page where the information will be inserted into the mysql database.
<script type="text/javascript">
// <![CDATA[
$(document).ready(function(){
// click to submit an event
$('#Create').click(function(){
var a = $("#EventInput").val();
if(a != "What are you planning?")
{
$.post("concorunt.php?val=1&id=<?php echo $id?>&"+$("#EventForm").serialize(), {
}, function(response){
$('#ShowEvents').prepend($(response).fadeIn('slow'));
clearForm();
});
}
else
{
alert("Enter event name.");
$("#EventInput").focus();
}
});
</script>
But nothing is happening.
You did not close your documentready function. Try this:
$(document).ready(function(){
// <![CDATA[ --> Either remove this or close this.
.....
.....
}); //Missing those braces in your code
im making a small script which gets some data from a database through php using jquery's getJSON method.
the code as shown below:
$(document).ready(function(){
var id = userid;
$('#a-div').after('<div id="data"></div><input type="button" id="getdata" value="Get Data">');
$('#getdata').click(function(){
$.getJSON('http://mysite.com/data.php?id=' + id, function(data) {
var notfound = data['notfound'];
var user = data['user'];
if(notfound == '1'){
$('#data').html("Not found");
}
else{
$('#data').html("Found , user is "+ user);
}
});//end of getJSON
});//end of click
}); //end of document ready
My php script returns JSON data something like this :
If the data is found in database-
{"notfound":"0","user":"john"}
If the data is NOT found in database-
{"notfound":"1","user":"none"}
This works perfectly on Firefox , Google Chrome and Safari , just dosent work in Internet Explorers(7,8,9)
can anyone help me out.
P/S i have tried a few techniques in other posts similar to this one , is not working.
Like changing the the META content-type
Thanks.
Try putting the button inside of the <body> like so:
$("body").append( /* markup for div and btn here */ );
...instead of using after().
Maybe internet explorer is taking long time to add the html, try with live
$('#getdata').live('click', function(){
you are using very ugly code, after body incorrect to write any HTML content, for that you must add in body any div and append your cod to it, and then this code will work in IEs
$(document).ready(function () {
$('#anydiv').html('<div id="data"></div><input type="button" id="getdata" value="Get Data">');
$('#getdata').click(function () {
alert("asd");
$.getJSON('/HowItWork/Index?id=' + 23, function (data) {
var notfound = data['notfound'];
var user = data['user'];
if (notfound == '1') {
$('#data').html("Not found");
}
else {
$('#data').html("Found , user is " + user);
}
}); //end of getJSON
}); //end of click
}); //end of document ready
This works for me- jQuery.support.cors = true;
Find more discussion http://jquery.10927.n7.nabble.com/jQuery-getJSON-response-not-working-with-IE-works-perfectly-in-firefox-safari-td81254.html