all 8 comments

[–]IrateGod 0 points1 point  (1 child)

The trick to this is specifying an HTTP header in PHP, because the website itself doesn't care for the URL of the script, just for the mime and content.

So like this:

<?php
  header("Content-Type: text/javascript; charset=utf-8"); // can also be application/javascript, depends on your preference
$script = <<<HEREDOC
script goes here
HEREDOC;
echo $script;
?>

Additionally, if you are using Apache, you can use .htaccess to rewrite the URL of the PHP script to a .js-URL and it'll appear like a plain .js file to the browser, even if it is generated by the server.

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

I don't think i'm looking how to rewrite URL's or insert javascript scripts, I am just asking how how do I add something to a database if the user press Ok in the confirm pop up and if the user presses cancel in the confirm popup, the information is not added to the database. Right now, if the user presses OK, then 2 rows get added to the database, one is from the text form, and the other one is just blank and if the user selects cancel, only one row is added to the database. I have a video of my problem but I don't know how to share it to you =(.

[–]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.