0

I want to display jquery sweet alert after php insert query successfully executed.. but whenever i tried to call jquery function after if condition, didn't work..or alert appear for 1-2 sec before page reload for form submit and disappear immediately..it only works with button onclick method..

<html>
<head>
<script src="lib/sweet-alert.js"></script>
<link rel="stylesheet" href="lib/sweet-alert.css">
</head>

<body>
<form method="post">
  <label for="name">name</label>
  <input type="text" name="name" id="name" />
  <input type="submit" value="submit" name="submit" class="sub" id="sub" />

<?php
   $a=mysql_connect("localhost","root","");
   $a1=mysql_select_db("test",$a);

  if(isset($_POST['submit']))
  {
         $name=$_POST['name'];
         $i=mysql_query("insert into student (name) values ('$name')");
    if($i)
    {

        echo "<script>a();</script>";
    }
  }
 ?>

 <script type="text/javascript">
 function a(){
 swal("Here's a message!");
 };
</script>

</body>
</html>
1

4 Answers 4

4

You are trying to mix two incompatiable languages, PHP is Server side, JQuery is Client Side. The PHP script is already finished processing once your Browser displays the page so you can not then decide to interact with the browser from a PHP script.

You need to use Ajax from a javascript page if you want this kind of functionality.

Sign up to request clarification or add additional context in comments.

2 Comments

That's rude! and not true as well. You can always echo responses through JS via PHP, given that the response is being echoed after either a page load, a redirection or an AJAX request!
can u tell me..how to use ajax??.i don't know ajax
0

Put javascript function declaration before your php script.

<script type="text/javascript">
 function a(){
 swal("Here's a message!");
 };
</script>
<?php
   $a=mysql_connect("localhost","root","");
   $a1=mysql_select_db("test",$a);

  if(isset($_POST['submit']))
  {
         $name=$_POST['name'];
         $i=mysql_query("insert into student (name) values ('$name')");
    if($i)
    {

        "<script>a();</script>";
    }
  }
 ?>

3 Comments

it didn't work..swal function for plugin can only be placed before closing body tag..
Put your full php file here. There is no plugin included in your codes here
And as @Ghost said, put echo before <script>a();</script> like this echo "<script>a();</script>"
0
<?php 
if($_REQUEST['submit']){
    $i = "yes";
    if($i == "yes"){
    echo "<script>alert('hiii');</script>";
    }
}
?>
<form action="" method="post">
<input type="submit" name="submit" value="submit">
</form>

4 Comments

I know..alert is working perfectly but jQuery plugin doesn't working. I want to use sweetalert jQuery plugin
I think something conflicting with your plugin try this: in Google chrome and Go to page which you want to see effect now press Ctrl+shift+j and see console check any error is there ? or give me live link
error: Uncaught TypeError: Cannot read property 'querySelector' of null
check out jquery plugin link.. tristanedwards.me/sweetalert.it is working with button but i want to display alert after php insert query ..with php it's not working.
0

I have 1 demo.php file for form and alert. And demo1.php file for php query. I have to use Ajax for this. demo.php:

<html>
<head>
<script src="lib/sweet-alert.js"></script>
<link rel="stylesheet" href="lib/sweet-alert.css">
<script src="js/jquery-1.8.0.min.js"></script>
</head>

<body>
<form method="post" class="frm" id="myform" onSubmit="j1">
 <label for="name">name</label>
 <input type="text" name="name" id="name" />
 <input type="submit" value="submit" name="submit" class="sub" id="sub" />
</form>

 <script>

     function j1(){
     var query = $('#myform').serialize();
     var url = 'demo1.php';
     $.post(url, query, function (response) {

         swal({
    title: "Thank You!",
    type: "success",
    confirmButtonColor: '#DD6B55',
    confirmButtonText: 'Okay'
    },
    function(isConfirm){
    if (isConfirm){
    window.location.replace('demo.php');
    } 
   });

   });

    }
   $("#myform").submit(function(){
    return false;
   });

  </script>
</body>
</html>

demo1.php:

<?php
   $a=mysql_connect("localhost","root","");
   $a1=mysql_select_db("test",$a);      

                 $name=$_POST['name'];
     $i=mysql_query("insert into student (name) values ('$name')");

 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.