Skip to content
Fix Code Error

How to call a PHP function on the click of a button

March 13, 2021 by Code Error
Posted By: Anonymous

I have created a page called functioncalling.php that contains two buttons, Submit and Insert.

I want to test which function is executed when a button gets clicked. I want the output to appear on the same page. So, I created two functions, one for each button.

<form action="functioncalling.php">
    <input type="text" name="txt" />
    <input type="submit" name="insert" value="insert" onclick="insert()" />
    <input type="submit" name="select" value="select" onclick="select()" />
</form>

<?php
    function select(){
        echo "The select function is called.";
    }
    function insert(){
        echo "The insert function is called.";
    }
?>

The problem here is that I don’t get any output after any of the buttons are clicked.

Where exactly am I going wrong?

Solution

Button clicks are client side whereas PHP is executed server side, but you can achieve this by using Ajax:

$('.button').click(function() {
  $.ajax({
    type: "POST",
    url: "some.php",
    data: { name: "John" }
  }).done(function( msg ) {
    alert( "Data Saved: " + msg );
  });
});

In your PHP file:

<?php
    function abc($name){
        // Your code here
    }
?>
Answered By: Anonymous

Related Articles

  • How to properly do JSON API GET requests and assign…
  • How to parse JSON with XE2 dbxJSON
  • Azure Availability Zone ARM Config
  • The 'compilation' argument must be an instance of…
  • Search match multiple values in single field in…
  • Avoid creating new session on each axios request laravel
  • Event Snippet for Google only shows one event while…
  • NullpointerException error while working with…
  • Why does this Azure Resource Manager Template fail…
  • loop and eliminate unwanted lines with beautiful soup
  • How do I limit the number of digits from 6 to 4 in…
  • mongodb group values by multiple fields
  • Octave using 'for' statement to show two animations…
  • Most effective way to parse JSON Objects
  • Azure CLI - az deployment group create -…
  • Does moment.js allow me to derive a timezone…
  • How to traverse the complex nested Json in C# and…
  • Angular 12 - Generating browser application bundles…
  • ./components/Avatar.tsx Error: Cannot find module…
  • C# Get YouTube videoId from Json
  • Python scrape JS data
  • Python JSON TypeError : list indices must be…
  • Siemens LOGO! PLC data in the wrong order
  • Can't upload files with Apollo-client GraphQL in…
  • How to build correlation matrix plot using specified…
  • error NG6002: Appears in the NgModule.imports of…
  • How do I remove single children in a tree?
  • One to many relationship in two JSON arrays, lookup…
  • Error Stack Overflow when trying to hide buttons in…
  • Need constraints for y position or height when…
  • Posting array of objects to REST API with ReactJS
  • TypeError: players.map is not a function
  • Function runs into an error after successfully…
  • state data can not be set using useState in reactjs
  • How to query Json field so the keys are the column…
  • Scraping text after a span in with Regex (and Requests)
  • How to blur the background after click on the button…
  • Python is not calling fucntions properly
  • Blazor Can't Update UI
  • python pandas - add unique Ids in column from master…
  • Fragment Not Showing in the Activity
  • Remove Text from the edit text when edit text is focused
  • Fix top buttons on scroll of list below
  • R - Apply function to list of lm summaries
  • How can i use `parse = T` and functions like round…
  • Vega-lite data transformation to un-nest objects
  • Split JSON python string to pass to function
  • Python - Read JSON - TypeError: string indices must…
  • How to read through or navigate through a json object?
  • How can I center all my contents in html?
  • Js Calculation based on radio button, and some problems
  • How to determine the current iPhone/device model?
  • Combining pyOSC with pyQT5 / Threading?
  • Need to split output into an javascript array
  • C# parse FHIR bundle - read resources
  • MaterialCardView is in front of NavigationView. How…
  • How to use Servlets and Ajax?
  • How to reverse $unwind or re-assemble after $lookup?
  • Conditional render label Color base on another field value
  • React function keeps refreshing page causing huge…
  • how to transform a JSON coming from an API into…
  • Updating products with fetch and input value fields
  • How do we split words from a html file using string…
  • Azure Sql : Unable to Replace HTML String
  • AWS CodeCommit With Multi-factor Authentication.…
  • Javascript filtering a nested array to exclude…
  • Errorbars and bar plots having different positions in ggplot
  • JSON Schema - How to apply conditionals based on…
  • Why is my Shopify App built with Next.js (React) so…
  • Python Inserting headers for CSV file when…
  • How do I get currency exchange rates via an API such…
  • What to change in circular barplot in R?
  • Svelte application not working on android version…
  • ng serve is checking nodemodules in previous directory
  • Merge list of results into a single variable with Python
  • Install slick carousel in VueJS project
  • I want to create a SQLite database like in the…
  • onClick for buttons on custom CardView
  • display other inputs based on the value of another…
  • ggplot by group with filter()
  • Getting a "TypeError" when trying to validate a form
  • Issues trying to install sylius/product-bundle to my…
  • Generate sequence of dates for given frequency as…
  • Why so much unused space, engine is skipping to a…
  • How to Insert cell reference in VBA code for…
  • My node.js json parse function throws error
  • Data not catching in multi step contact form
  • How to get device make and model on iOS?
  • React eslint error missing in props validation
  • JSONLD context for duplicate keys at nested level
  • Post to PHP script after successful paypal payment
  • Bootstrap Form Formatting
  • How to sort elasticsearch buckets by timestamp…
  • Jenkins with nginx using docker Port 50000 config
  • Unable to move from one fragment to another
  • Multiple paired permutation t-tests using perm.t.test
  • model validation error message isn't working And…
  • How do I modify fields inside the new PostgreSQL…
  • Remove geom_point from certain geom_line in R ggplot2
  • Union of multiple Database queries with same parameters

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to leave/exit/deactivate a Python virtualenv

Next Post:

Is there a command to list all Unix group names?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error