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 7792021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:04:07+00:00 2026-06-01T22:04:07+00:00

Purpose: Call a PHP function to read data from a file and rewrite it.

  • 0

Purpose:
Call a PHP function to read data from a file and rewrite it. I used PHP only for this purpose – FileIO – and I’m new to PHP.

Solution?
I tried through many forums and knew that we cannot achieve it normal way: onClick event > call function. How can we do it, are there other ways, particularly in my case?
My HTML code and PHP code is on the same page: Admin.php.
This is HTML part:

<form>
    <fieldset>
        <legend>Add New Contact</legend>
        <input type="text" name="fullname" placeholder="First name and last name" required /> <br />
        <input type="email" name="email" placeholder="etc@company.com" required /> <br />
        <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br />
        <input type="submit" name="submit" class="button" value="Add Contact" onClick="" />
        <input type="button" name="cancel" class="button" value="Reset" />
    </fieldset>
</form>

This is PHP part:

function saveContact()
{
    $datafile = fopen ("data/data.json", "a+");
    if(!$datafile){
        echo "<script>alert('Data not existed!')</script>";
    } 
    else{
        ...
        $contact_list = $contact_list . addNewContact();
        ...
        file_put_contents("data/data.json", $contact_list);
    }

    fclose($datafile);
}

function addNewContact()
{
   $new = '{';
   $new = $new . '"fullname":"' . $_GET['fullname'] . '",';
   $new = $new . '"email":"' . $_GET['email'] . '",';
   $new = $new . '"phone":"' . $_GET['phone'] . '",';
   $new = $new . '}';
   return $new;
}

Have a look at these code, I want to call saveContact when people click on Add Contact button. We can reload page if need so. FYI, I use JQuery, HTML5 in page as well.
Thanks,

  • 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-06-01T22:04:08+00:00Added an answer on June 1, 2026 at 10:04 pm

    There are two ways. the first is to completely refresh the page using typical form submission

    //your_page.php
    
    <?php 
    
    $saveSuccess = null;
    $saveMessage = null;
    
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
      // if form has been posted process data
    
      // you dont need the addContact function you jsut need to put it in a new array
      // and it doesnt make sense in this context so jsut do it here
      // then used json_decode and json_decode to read/save your json in
      // saveContact()
      $data = array(
        'fullname' = $_POST['fullname'],
        'email' => $_POST['email'],
        'phone' => $_POST['phone']
      );
    
      // always return true if you save the contact data ok or false if it fails
      if(($saveSuccess = saveContact($data)) {
         $saveMessage = 'Your submission has been saved!';     
      } else {
         $saveMessage = 'There was a problem saving your submission.';
      } 
    }
    ?>
    
    <!-- your other html -->
    
    <?php if($saveSuccess !== null): ?>
       <p class="flash_message"><?php echo $saveMessage ?></p>
    <?php endif; ?>
    
    <form action="your_page.php" method="post">
        <fieldset>
            <legend>Add New Contact</legend>
            <input type="text" name="fullname" placeholder="First name and last name" required /> <br />
            <input type="email" name="email" placeholder="etc@company.com" required /> <br />
            <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br />
            <input type="submit" name="submit" class="button" value="Add Contact" onClick="" />
            <input type="button" name="cancel" class="button" value="Reset" />
        </fieldset>
    </form>
    
    <!-- the rest of your HTML -->
    

    The second way would be to use AJAX. to do that youll want to completely seprate the form processing into a separate file:

    // process.php

    $response = array();
    
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
      // if form has been posted process data
    
      // you dont need the addContact function you jsut need to put it in a new array
      // and it doesnt make sense in this context so jsut do it here
      // then used json_decode and json_decode to read/save your json in
      // saveContact()
      $data = array(
        'fullname' => $_POST['fullname'],
        'email' => $_POST['email'],
        'phone' => $_POST['phone']
      );
    
      // always return true if you save the contact data ok or false if it fails
      $response['status'] = saveContact($data) ? 'success' : 'error';
      $response['message'] = $response['status']
          ? 'Your submission has been saved!'
          : 'There was a problem saving your submission.';
    
      header('Content-type: application/json');
      echo json_encode($response);
      exit;
    }
    ?>
    

    And then in your html/js

    <form id="add_contact" action="process.php" method="post">
            <fieldset>
                <legend>Add New Contact</legend>
                <input type="text" name="fullname" placeholder="First name and last name" required /> <br />
                <input type="email" name="email" placeholder="etc@company.com" required /> <br />
                <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br />
                <input id="add_contact_submit" type="submit" name="submit" class="button" value="Add Contact" onClick="" />
                <input type="button" name="cancel" class="button" value="Reset" />
            </fieldset>
        </form>
        <script type="text/javascript">
         $(function(){
             $('#add_contact_submit').click(function(e){
                e.preventDefault();  
                $form = $(this).closest('form');
    
                // if you need to then wrap this ajax call in conditional logic
    
                $.ajax({
                  url: $form.attr('action'),
                  type: $form.attr('method'),
                  dataType: 'json',
                  success: function(responseJson) {
                     $form.before("<p>"+responseJson.message+"</p>");
                  },
                  error: function() {
                     $form.before("<p>There was an error processing your request.</p>");
                  }
                });
             });         
         });
        </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a general-purpose way to make GET requests with jQuery: var loadUrl="mypage.php"; $("#get").click(function(){
I try to call the esriRegAsm.exe with arguments from a C# program. The purpose
The purpose of this code is to pull upgrade.zip from a central server, extract
I am trying to port a php file that is used with MySQL. My
I hava some UILabel in UITableViewCell. My purpose is to call a function when
What is the purpose of the Call Stack window in Visual Studio?
Purpose: I plan to Create a XML file with XmlTextWriter and Modify/Update some Existing
[purpose] This simple command sequence runs expected in the Windows' CMD shell: dir &
Trying to autoload classes from <root>/incl/classes folder. The problem is, when I call some
If I save some object in a php session variable $_SESSION['geoip'] = new GeoIP();

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.