I basically have this if clause written on javascript - A confirm box pops up, and if the user clicks "ok", certain data is inserted into the database, otherwise, it does not. Here's the code: <script type="text/javascript"> if(window.confirm("Tem a certeza que quer reservar <?php echo "$quantidade_produto" ?> unidades conjuntas de <?php echo "$nome_produto" ?> no valor de <?php echo "$preco_final" ?>€?")){ <?php mysql_query("INSERT INTO encomendas VALUES('',$data,$preco_final,$quantidade_produto,$id_produto,'$user')",$connection); ?> alert("Reserva efectuada. Será em breve contactado por nós. Obrigado pela sua preferência."); window.location.href='produtos.php';} else{ window.location.href='produtos.php';} </script> PHP: However, the data is inserted into the database regardless of the option the user chooses. Why is this? Help would be appreciated.
From what I can tell you have a bad mixture of Javascript and PHP. Think about it, the languages are two completely different things. An if statement in Javascript is not going to regard any PHP code. The PHP code is going to be interpreted regardless if the if condition became true because the PHP parser is not looking for Javascript. In summary, you can't integrate languages like that. You'll probably want to throw in some AJAX that calls a separate PHP script to do the entries in the MySQL database.
To answer DomainCo.US' question, no, the alert doesn't show up, so I'll take it AliasXNew's interpretation is correct* - Thing is, I don't know anything about Ajax and don't really have time to learn it and finish what I'm doing at the same time - So, are there anyways around it? Suggestions would be appreciated. *Note: But I did manage to mix javascript with PHP by echoing the PHP variables inside the javascript confirmation box: window.confirm("Are you sure you want to order <?php echo "$product_quantity" ?> pieces of <?php echo "$name_product" ?> in the value of <?php echo "$final_price" ?>€?") PHP: And it works perfectly - So, why is here different?
Understand that you're not actually intergrating PHP and JS when you output variable data. Think of it like this: 1) Someone requests your PHP page 2) The server grabs the page and hands it to the PHP interpreter which interprets any PHP code 3) At this point your above line: window.confirm("Are you sure you want to order <?php echo "$product_quantity" ?> pieces of <?php echo "$name_product" ?> in the value of <?php echo "$final_price" ?>€?") Code (markup): Would now look like: window.confirm("Are you sure you want to order 10 pieces of Some Product in the value of 100€?") Code (markup): Assuming those happen to be the variable values. 4) The server sends the modified content back 5) Firefox executes the Javascript You see what happens? The PHP is interpreted way before the Javascript, so theoretically it's impossible to integrate the two. Honestly I can't think of any better ways than AJAX. It might sound scary, but AJAX in itself is not even a language, just a type of usage of Javascript. Google "PHP and AJAX" and you'll come across several well written tutorials that will explain exactly what I was referring to in using AJAX to call PHP pages.
Ok i got it, thanks. So what i would want is the javascript to execute at the same level as the PHP code, so it would switch client-server-client (JS-PHP-JS)? Does Ajax enable this? In which way?
Well you can't make JS execute on the same level as PHP as PHP is strictly server side and JS is strictly client side. AJAX works by making requests to a page thus executing the code. In your case you could have AJAX request "insert.php" and give off the proper parameters (i.e "insert.php?name=blah&quantity=blah") and then in insert.php do the actual MySQL query. Just make sure you take into account some of the possible security vulnerabilities.