• <bdo id='Z6zxK'></bdo><ul id='Z6zxK'></ul>
  1. <tfoot id='Z6zxK'></tfoot>
  2. <small id='Z6zxK'></small><noframes id='Z6zxK'>

  3. <i id='Z6zxK'><tr id='Z6zxK'><dt id='Z6zxK'><q id='Z6zxK'><span id='Z6zxK'><b id='Z6zxK'><form id='Z6zxK'><ins id='Z6zxK'></ins><ul id='Z6zxK'></ul><sub id='Z6zxK'></sub></form><legend id='Z6zxK'></legend><bdo id='Z6zxK'><pre id='Z6zxK'><center id='Z6zxK'></center></pre></bdo></b><th id='Z6zxK'></th></span></q></dt></tr></i><div id='Z6zxK'><tfoot id='Z6zxK'></tfoot><dl id='Z6zxK'><fieldset id='Z6zxK'></fieldset></dl></div>

    <legend id='Z6zxK'><style id='Z6zxK'><dir id='Z6zxK'><q id='Z6zxK'></q></dir></style></legend>

      在 onClick HTML 事件之后调用 PHP 函数

      Call a PHP function after onClick HTML event(在 onClick HTML 事件之后调用 PHP 函数)

      <tfoot id='FhgyO'></tfoot>
      1. <small id='FhgyO'></small><noframes id='FhgyO'>

      2. <legend id='FhgyO'><style id='FhgyO'><dir id='FhgyO'><q id='FhgyO'></q></dir></style></legend>

            <bdo id='FhgyO'></bdo><ul id='FhgyO'></ul>
              <tbody id='FhgyO'></tbody>
            • <i id='FhgyO'><tr id='FhgyO'><dt id='FhgyO'><q id='FhgyO'><span id='FhgyO'><b id='FhgyO'><form id='FhgyO'><ins id='FhgyO'></ins><ul id='FhgyO'></ul><sub id='FhgyO'></sub></form><legend id='FhgyO'></legend><bdo id='FhgyO'><pre id='FhgyO'><center id='FhgyO'></center></pre></bdo></b><th id='FhgyO'></th></span></q></dt></tr></i><div id='FhgyO'><tfoot id='FhgyO'></tfoot><dl id='FhgyO'><fieldset id='FhgyO'></fieldset></dl></div>

                本文介绍了在 onClick HTML 事件之后调用 PHP 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                目的:调用 PHP 函数从文件中读取数据并重写它.我仅将 PHP 用于此目的 - FileIO - 我是 PHP 新手.

                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.

                解决方案?我尝试了很多论坛,知道我们无法正常实现:onClick事件>调用函数.我们该怎么做,还有其他方法吗,尤其是在我的情况下?我的 HTML 代码和 PHP 代码在同一页面上:Admin.php.这是 HTML 部分:

                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>
                

                这是 PHP 部分:

                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;
                }
                

                看看这些代码,当人们点击添加联系人按钮时,我想调用 saveContact.如果需要,我们可以重新加载页面.仅供参考,我也在页面中使用 JQuery、HTML5.谢谢,

                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,

                推荐答案

                有两种方法.第一种是使用典型的表单提交来完全刷新页面

                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 -->
                

                第二种方法是使用 AJAX.为此,您需要将表单处理完全分离到一个单独的文件中:

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

                //进程.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;
                }
                ?>
                

                然后在你的 html/js 中

                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>
                

                这篇关于在 onClick HTML 事件之后调用 PHP 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                PHP Upload File Validation(PHP 上传文件验证)
                PHP Error - Uploading a file(PHP 错误 - 上传文件)
                How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)
                <legend id='GRX0S'><style id='GRX0S'><dir id='GRX0S'><q id='GRX0S'></q></dir></style></legend>
                  <tfoot id='GRX0S'></tfoot>
                    <tbody id='GRX0S'></tbody>
                • <i id='GRX0S'><tr id='GRX0S'><dt id='GRX0S'><q id='GRX0S'><span id='GRX0S'><b id='GRX0S'><form id='GRX0S'><ins id='GRX0S'></ins><ul id='GRX0S'></ul><sub id='GRX0S'></sub></form><legend id='GRX0S'></legend><bdo id='GRX0S'><pre id='GRX0S'><center id='GRX0S'></center></pre></bdo></b><th id='GRX0S'></th></span></q></dt></tr></i><div id='GRX0S'><tfoot id='GRX0S'></tfoot><dl id='GRX0S'><fieldset id='GRX0S'></fieldset></dl></div>
                    <bdo id='GRX0S'></bdo><ul id='GRX0S'></ul>

                      1. <small id='GRX0S'></small><noframes id='GRX0S'>