Javascript not working in sending data - javascript

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

Related

PHP code inside script tag for redirecting to another page

I have a list of users besides that are the delete button. Where in when you click the delete button a confirm() script will appear. I used this code but the one being deleted is always the last id.
button onclick="myFunction()">Delete</button></td>
<script>
function myFunction() {
if(confirm("Are you sure you want to delete this?") == true) {
window.location.href = "delete.php?id=<?php echo $r->id); ?>";
} else {
//
}
}
</script>
You can use this:
<button onclick="myFunction(<?php echo $r->id; ?>)">Delete</button></td>
<script>
function myFunction(id) {
if(confirm("Are you sure you want to delete this?") == true) {
window.location.href = "delete.php?id="+id;
} else {
//
}
}
</script>

PHP Script on back-end when click on button using JQuery

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>

Why the success alert message is appearing many times in an AJAX call made to a PHP script in following scenario?

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) {

Jquery box on page load on php

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.

How to submit login form using jQuery

I have a login form. When users press login, the jQuery checks if the fields are empty. If they are not empty id like to submit my form to a page called login_action.php. I am unsure how to do this.
Here's my JavaScript code:
<script type='text/javascript'>
$(document).ready(function() {
$('#login').click(function() {
if(!$(':text').val()) {
$('#errors').text('enter a username and password');
} else {
// This is where I'd like to redirect
}
});
});
</script>
Just submit the form:
$('#yourformsid').submit();
First you should set attributes action='login_action.php' and id='myFormId' to the form tag, and then modify your code as:
<script type='text/javascript'>
$(document).ready(function(){
$('#login').click(function(){
if (!$(':text').val()){
$('#errors').text('enter a username and password');
} else {
//this is where i'd like to redirect
$("#myFormId").submit();
}
});
});
</script>
PS: the #login button must not be type of submit but button. or if it is submit, you should add return false; if you don't want redirect and return true; it will post/get the form to the address that you've set in action attribute.
window.location = "http://www.google.com/"
read more here:
http://www.tizag.com/javascriptT/javascriptredirect.php

Categories