Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:22:24+00:00 2026-05-30T10:22:24+00:00

I need help with adding to a database. I want to call a javascript

  • 0

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)
    }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:22:26+00:00Added an answer on May 30, 2026 at 10:22 am

    So, this "works" the same way that hammering a screw in does. It will get in there, but not really the way you want to do things.

    Don’t use a script tag to trigger a php page. Your page will trigger, but it is the wrong tool to get the job done. You want to use AJAX. AJAX was specifically made for this purpose while a script tag was meant for running scripts on the page. It will try to fetch the contents of addtofavorites.php but it will expect JavaScript as the return. And you won’t be able to use $_POST since you have no way to post data to the request.

    Instead, use AJAX. If you are already using a JavaScript library, then it will have a nice AJAX wrapper for you (I don’t know of a single library off the top of my head that doesn’t). You can check out the API documentation for you given library for documentation on how to use the AJAX functionality (jQuery, Prototype.js, Mootools, etc.).

    As an example, I will use jQuery, because it is one of the most popular ones. Here is a request using jQuery to your page (and what looks to be expected variables)

    // Call an AJAX function to the proper page
    $.ajax("php/addtofavorites.php", {
            // Pass our data to the server
            data: { "get" : "runfunction", "action" : "favorites1" },
            // Pass using the appropriate method
            method: "POST",
            // When the request is completed and successful, run this code.
            success: function (response) {
                    // Successfully added to favorites. JS code goes here for this condition.
                }
        });
    

    Also, as a side note, every line in PHP needs a semicolon ; at the end of it. Many of yours don’t. And the 3rd line is missing an opening bracket { though I’m not sure if that is just an artifact of your copy paste or not.

    Addendum

    I would recommend using jQuery to listen for events as well. The events API for jQuery will let you listen for events on objects. So, something like this:

    <input id="button_1" type="button" value="favorites1" />
    

    and the jQuery

    $(document).ready(function () { // Make sure the elements are loaded on the page
        // Listen for a click event on the button
        $('#button_1').click(favfunct);
    });
    // Now define the function
    function favfunct(e) {
        // Stop the page from "following" the button (ie. submitting the form)
        e.preventDefault();
        e.stopPropagation();
        // Insert AJAX call here...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help writing a script downloads data from google insight using c# this is
I need some help adding conditions to a linq query. Take this small example
I need a little help clearing something up with Zend_Form and adding filters to
I created a database of adding employees now i need to notify(push notification) the
I need help in designing a database model for my company. basically it is
I need some help adding some items to a QComboBox . So I have
need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I need help understanding some C++ operator overload statements. The class is declared like
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.