0

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

user3272713
  • 167
  • 2
  • 5
  • 15
  • I don't see any HTML in add.php. Additionally, using the Jquery code above will stop the header() from redirecting properly, since redirects only work if there is no HTML output at all (even a space will stop it from working). – Victoria Ruiz Feb 14 '14 at 00:34
  • but how can I solve this? How can I output an pop up message to tell the user that he can not add activity on that date – user3272713 Feb 14 '14 at 00:37

1 Answers1

0

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.

Community
  • 1
  • 1
Victoria Ruiz
  • 4,913
  • 3
  • 23
  • 40