I need help with adding to a database. I want to call a javascript scrt from a button click method. The Javascript Script, I want to call upon a php file which contains some code that adds to a MySQL Database.
I literally tried over 20+ website and No help with the stuff.
If AJAX would be better Could you help me?
The Button Code:
<input type="button" value="favorites1" onClick="favfunct();"> 
The Javascript Code
function favfunct() {
    var target = document.createElement( "script" );
    target.setAttribute( "src", "php/addtofavorites.php" );
    document.getElementsByTagName( "body" )[0].appendChild( target );`
}
The Php Code
<?php
    echo "test successful"
    $con = mysql_connect("localhost","root","student");
    if ($_POST["get"] == 'runfunction')
        echo "Works";
    }
    if ($_POST["action"] == 'favorites1'){ 
        echo "Testing Was Successful"
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }                   
        mysql_select_db("tvid", $con);
        $sql="INSERT INTO Persons (userid, davorites) VALUES 
            ('1','$_POST[video0]')";
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
        echo "Your Video was Added To Your Favorites";
        mysql_close($con)
    }
?>
 
     
     
    