1

I've looked at all the questions on here and simply cannot get my head around this or get it to work. I simply want to use a button which runs a script that executes a php function.

HTML:

<button id="update_submit" type="button" onClick="myFunction()">Update</button>

Script:

                    <script>
                        function myFunction(){
                            if (document.getElementById("drug_text_name").value=="") {
                                alert( "I am an alert box!" );
                            } else {
                            $.ajax({url: "../script.php", success: function(result){
                                window.location.href = '../drug_manager.php';
                                }      
                                   });
                            }
                        }
                    </script>

PHP file:

<?php
// including the database connection file

    include_once("../../includes/dbh.inc.php");
    $result = mysqli_query($conn, "UPDATE drugs SET Drug_Name='$Drug_Name' ,Allergies='$Allergies' ,Side_effects='$Side_effects' ,Type_of_Medication='$Type_of_Medication',Dosage='$Dosage' WHERE Drug_ID=$Drug_ID");

?>

Here, I would like it to run the SQL query, then redirect the user to the page /drug_manager.php.

AF1
  • 17
  • 5
  • Your php file is not returning json which `dataType: 'json'` says it should. If you don't return anything, your ajax call will bomb since you are telling it it **will** return json – Taplar May 01 '20 at 18:21
  • Also, that php file is not executing that function...... ? – Taplar May 01 '20 at 18:21
  • 1
    Why not POST to PHP ? – devlin carnate May 01 '20 at 18:21
  • @Taplar What should I put there instead of dataType: – AF1 May 01 '20 at 18:21
  • You should address devlin's comment. Are you wanting to redirect the page? If so, using ajax doesn't make sense. And if an endpoint is not returning json, don't put the dataType there. You are not required to specify the dataType. – Taplar May 01 '20 at 18:22
  • @devlincarnate I copy/pasted this from someone elses solution – AF1 May 01 '20 at 18:24
  • @Taplar I'm wanting to redirect it simply as a test, so I can just make sure its working, I'm using a different function for it after I get it to work – AF1 May 01 '20 at 18:25

1 Answers1

3

First of all, you cannot execute a PHP-function within JavaScript because PHP runs on the server-side and JavaScript runs on the client-side. In your example, your PHP-file is not doing anything because the test function is never executed. If you execute the test function the ajax call will be redirected to that location, read more about it here.

If you want the user to be redirected when they click the button you could simply redirect them with help of window.location.href, something like this:

<script>
$("#update_submit").on('click', function(){
    window.location.href = '../script.php';
)};
</script>

If you want PHP to decide where the user should be redirected your PHP-file could return an URL and then you use window.location.href to that URL. Something like this:

PHP:

<?php
function test() {
    echo "drug_manager.php";
}
test();
?>

Script:

<script>
$("#update_submit").on('click', function(){

    $.ajax({
       url: '../script.php',
       dataType: 'json',
       success: function(data){
           window.location.href = data;
       }
    });
)};
</script>
mikaelwallgren
  • 310
  • 2
  • 9
  • Since I posted the question I changed my method, would you be able to please take a look at that? I would really appreciate it since I'm stumped at the moment. – AF1 May 01 '20 at 19:29
  • 1
    Instead of sending an ajax request to script.php you could send the user to script.php using window.location.href = '../script.php'. In script.php you can add a line in the bottom of the script that redirects the user to drug_manager.php. Add this as the last line in your php script: header('Location: drug_manager.php'); – mikaelwallgren May 01 '20 at 19:46
  • That's an approach I did not know of. This is what I have right now: function myFunction() { if (document.getElementById("drug_text_name").value=="") { alert( "I am an alert box!" ); } else { window.location.href = '../script.php'; } } From what I can tell, it doesn't seem to run the SQL queries – AF1 May 01 '20 at 20:10
  • 1
    What does your PHP script look like? – mikaelwallgren May 01 '20 at 20:23
  • I've just set up error logs, these are the errors I get: https://pastebin.com/Ua3vuMP4 – AF1 May 01 '20 at 20:39
  • 1
    Ok, so in this case, you're expecting a POST request and when you redirect someone to a page they will do a GET request. Do you have a HTML-form? The easiest way would be to post a html form like this: https://codepen.io/mikaelwallgren/pen/bGVoKJb – mikaelwallgren May 01 '20 at 20:50
  • Yes, I do have a HTML form which shows drug data (as I'm implementing the edit form for this). – AF1 May 01 '20 at 21:11