0

I was trying to display a pop up message using sweet alert function after successful form data submission. My data is getting inserted into database but after successful insertion message including php variable($Name,$state) not displaying. If I use normal alert function it is working file.I asked this question at this forum previously Previous question asked and as per their recommendation i tried but no joy kindly help me to find out what is wrong in my code.

<?php
session_start();
error_reporting(0);
include('config.php');
if(isset($_POST['create']))
{
$Name=$_POST['Name'];
$state=$_POST['state'];

$sql="INSERT INTO  tblstudent(Name,state) VALUES('$Name','$state')";
$query = $dbh->prepare($sql);
echo $sql;
echo $Name;
echo $state;
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId)
{
    echo '
    <script>
        $(document).ready(function(){
            swal({
                title: "Successfully save! ${$Name}/${$state} ",
                html: "Your request for ${$Name}/${$state}  has been successfully filled",
                timer: 5000,
                timerProgressBar: true,
            }).then(function() {
                window.location = "index.php";
            });
        });
    
</script>
';
 
}
else 
{
$_SESSION['error']="Something went wrong. Please try again";
header('location:manage-region2.php');
}

}
?>
<!DOCTYPE html>
<html >
<head>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>  
</head>
<body>
<div class="panel-body">
<form role="form" method="post">
<div class="form-group">
<label>Name</label>
<input class="form-control" type="text" name="Name" autocomplete="off" required />
</div>
<div class="form-group">
<label>State Name</label>
<input class="form-control" type="text" name="state" autocomplete="off" required />
</div>
<button type="submit" name="create" class="btn btn-info">Create </button>
</form>
</div>
</body>
</html>
<?php  ?>
  
gorakh
  • 59
  • 3
  • Firstly does PHP actually definitely echo that script tag? Secondly, if it does, are there any errors in the browser's console about it? You haven't really provided any useful debugging information and we of course cannot run your code. – ADyson Apr 05 '23 at 17:51
  • P.S. **Warning:** Your code is vulnerable to SQL Injection attacks. You should use prepared statements **with parameters** to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli / PDO. **Never** insert unparameterised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Apr 05 '23 at 17:51
  • https://phpdelusions.net also contains good examples of writing safe SQL using mysqli and PDO. See also: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, check it again carefully because you've used a prepared statement, but missed the step where you need to use parameters. – ADyson Apr 05 '23 at 17:52
  • @ADyson it is showing error " Uncaught ReferenceError: $ is not defined" I have already included jquery library – gorakh Apr 05 '23 at 17:54
  • Evidently either a) jquery didn't load properly (check your network tool in the browser) and/or b) the order of execution is wrong and this code is running before the jquery script tag in the page. – ADyson Apr 05 '23 at 17:58
  • The same code i am using and My network speed is also good. Can you guide more or help me by pointing out my mistake. – gorakh Apr 05 '23 at 18:01
  • @ADyson If I use normar js alert it works fine. – gorakh Apr 05 '23 at 18:14
  • Nothing to do with network _speed_, it would only be an issue that way if the jquery file wasn't accessible for some reason. – ADyson Apr 05 '23 at 18:17
  • But it's quite clear that the script tag you're echoing is included in the page before the jquery file, therefore it will execute first, before jquery is available. Browsers execute scripts immediately as they are loaded into the page... they don't wait for everything else to be ready. In fact, it will be before you have even properly started the html document at all. Instead of echoing it immediately, put that string in a php variable and echo it later. – ADyson Apr 05 '23 at 18:18
  • @Adyson There is some problem with the code .bcs if i use this sweet alert at the end on page loading it works fine .It only creates problem when i use it after completion of mysql query. Can u try it at your end . – gorakh Apr 05 '23 at 18:20
  • `If I use normar js alert it works fine.`...yes because that's a built in JS functions and doesn't require the additional jquery library to be loaded – ADyson Apr 05 '23 at 18:20
  • `It only creates problem when i use it after completion of mysql query`...yes, because at that point jquery isn't loaded yet, but it needs jquery. Don't let php echo it until later, as I already suggested above. – ADyson Apr 05 '23 at 18:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252995/discussion-between-adyson-and-gorakh). – ADyson Apr 05 '23 at 18:22

0 Answers0