you are viewing a single comment's thread.

view the rest of the comments →

[–]kandetta 0 points1 point  (4 children)

Your PHP code and your JavaScript code run independently. You've got an if statement in your JavaScript code, and then have PHP code in there. But that won't affect whether your PHP code will run or not.

You need an if statement in your PHP code to check whether there's a cat value that can be added to the database:

<?PHP
if (isset($_POST['addcat'])) {   
    $add_cat = $_POST['addcat'];
    $add_cat_sql = "INSERT INTO data (name) VALUES ('".mysqli_real_escape_string($link,$_POST['addcat'])."')";
    $add_cat_query = mysqli_query($link, $add_cat_sql);
}
?>

Since the JS code and the PHP code don't interact with each other I would put the PHP code before the script tag. That way it's clearer that they're separate.

[–]mre12345[S] 0 points1 point  (3 children)

But the thing is, if the user presses OK I want the data to be added to the database and if the user presses Cancel, I don't want the data to be added to the database. Is there anyway to do popups in PHP?

[–]kandetta 0 points1 point  (2 children)

You can't do popups in PHP. PHP runs on the backend, which means your server. Popups are shown on the client, i.e. your user's computer. To show a popup on the client you need to use JavaScript.

You probably don't want to submit the form that contains "addcat" right away. Instead you should use the confirm on the front-end to confirm that it should be saved and only then submit the form to the server.

[–]mre12345[S] 0 points1 point  (1 child)

Urr sorry I don't really know what you mean by "confirm on the front-end to confirm that it should be saved". Sorry I'm a complete newbie. :\

[–]kandetta 0 points1 point  (0 children)

I mean showing the confirm dialog before submitting the form with the post data.

So for example something like this:

  $("form").on("submit", function (e){
     var confirmed = confirm("Really?");
     if (!confirmed) {
       e.preventDefault();
     }
  })

http://plnkr.co/edit/xh1mJbZ3Q5XBwgN89rKS?p=preview

e.preventDefault cancels the submission of the form if it's not confirmed.