最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Form doesnt save to database

programmeradmin2浏览0评论

It is my first form trying on wordpress.I changed some names such as "name","e-mail" etc. to "name1" , "email1" etc.Because without these changing, submit button refers to 404 page.

There is no any registration on database when I check it

I dont know any idea about what is the spesific code as problem so I give my all codes.If you know enough information, probably you can solve this easyly.But if you dont enough information, probably you will give negative point to question and go away from my question.Or maybe, you can close my question and write "give us spesific code/question".I write again, there is no problem with my question or asking style, it is completly your information.

<!-- data mata -->
    <div class="reservation-info">
        <form class="reservation-form" method="post">
            <h2>Make a reservation</h2>
            <div class="field">
                <input type="text" name="name1" placeholder="Name" required>
            </div>
            <div class="field">
                <input type="datetime-local" name="date1" placeholder="Date" step="300" required>
            </div>
            <div class="field">
                <input type="text" name="email1" placeholder="E-Mail">
            </div>
            <div class="field">
                <input type="tel" name="phone1" placeholder="Phone Number" required>
            </div>

            <div class="field">
                <textarea name="message1" placeholder="Message" requires></textarea>
            </div>

            <div class="g-recaptcha" data-sitekey="6LeKTjMUAAAAAJuZI0qqIBRp92slJoG4SESblWHw"></div>

            <input type="submit" name="reservation1" class="button" value="Send">

            <input type="hidden" name="hidden" value="1">
        </form>
    </div>

<?php 
    function lapizzeria_database(){
        global $wpdb;

        global $lapizzeria_db_version;
        $lapizzeria_db_version = "1.0";

        $table = $wpdb->prefix . 'reservations1';

        $charset_collate = $wpdb->get_charset_collate();

        // SQL Statement

        $sql = "CREATE TABLE $table ( 
                id mediumint(9) NOT NULL AUTO_INCREMENT, 
                name1 varchar(50) NOT NULL,
                date1 datetime NOT NULL,
                email1 varchar(50) DEFAULT '' NOT NULL,
                phone1 varchar(10) NOT NULL,
                message1 longtext NOT NULL,
                PRIMARY KEY (id)
        ) $charset_collate; ";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

    add_action('after_setup_theme', 'lapizzeria_database');

function lapizzeria_save_reservation() {

    if(isset($_POST['reservation1']) && $_POST['hidden'] == "1") {
              // read the value from recaptcha response
              $captcha = $_POST['g-recaptcha-response'];

              // Send the values to the server
              $fields = array(
                  'secret' => '6LeKTjMUAAAAAFeaj6Hq941AFvASw9sBJjeiCDyB',
                  'response' => $captcha,
                  'remoteip' => $_SERVER['REMOTE_ADDR']
              );

              // Start the request to the server

              $ch = curl_init('');

              // configure the request
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch, CURLOPT_TIMEOUT, 15);

              // Send the encode values in the URL 
              curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

              // Read the return value
              $response = json_decode(curl_exec($ch));
              if($response->success) {
                          global $wpdb;
                          $name = sanitize_text_field( $_POST['name1'] ) ;
                          $date = sanitize_text_field( $_POST['date1'] ) ;
                          $email = sanitize_email( $_POST['email1'] );
                          $phone = sanitize_text_field( $_POST['phone1'] ) ;
                          $message = sanitize_text_field( $_POST['message1'] ) ;

                          $table = $wpdb->prefix . 'reservations1';

                          $data = array(
                            'name1' => $name1,
                            'date1' => $date1,
                            'email1' => $email1,
                            'phone1' => $phone1,
                            'message1' => $message1
                          );

                          $format = array(
                            '%s',
                            '%s',
                            '%s',
                            '%s',
                            '%s'
                          );
                          $wpdb->insert($table, $data, $format );

                          $url = get_page_by_title('Thanks for your reservation!');
                          wp_redirect( get_permalink($url) );
                          exit();
              }


    }
}

add_action('init', 'lapizzeria_save_reservation');

 ?>

function lapizzeria_reservations() { ?>
  <div class="wrap">
      <h1>Reservations</h1>
      <table class="wp-list-table widefat striped">
          <thead>
              <tr>
                  <th class="manage-column">ID</th>
                  <th class="manage-column">Name</th>
                  <th class="manage-column">Date of Reservation</th>
                  <th class="manage-column">Email</th>
                  <th class="manage-column">Phone Number</th>
                  <th class="manage-column">Message</th>
                  <th class="manage-column">Delete</th>
              </tr>
          </thead>

          <tbody>
              <?php 
                global $wpdb;
                $table = $wpdb->prefix . 'reservations1';
                $reservations = $wpdb->get_results("SELECT * FROM $table", ARRAY_A); 
                foreach($reservations as $reservation): ?>
                    <tr>
                        <td><?php echo $reservation['id']; ?></td>
                        <td><?php echo $reservation['name1']; ?></td>
                        <td><?php echo $reservation['date1']; ?></td>
                        <td><?php echo $reservation['email1']; ?></td>
                        <td><?php echo $reservation['phone1']; ?></td>
                        <td><?php echo $reservation['message1']; ?></td>
                        <td>
                            <a href="#" class="remove_reservation" data-reservation="<?php echo $reservation1['id']; ?>">Remove</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
          </tbody>
      </table>
  </div>



<?php }


 ?>

It is my first form trying on wordpress.I changed some names such as "name","e-mail" etc. to "name1" , "email1" etc.Because without these changing, submit button refers to 404 page.

There is no any registration on database when I check it

I dont know any idea about what is the spesific code as problem so I give my all codes.If you know enough information, probably you can solve this easyly.But if you dont enough information, probably you will give negative point to question and go away from my question.Or maybe, you can close my question and write "give us spesific code/question".I write again, there is no problem with my question or asking style, it is completly your information.

<!-- data mata -->
    <div class="reservation-info">
        <form class="reservation-form" method="post">
            <h2>Make a reservation</h2>
            <div class="field">
                <input type="text" name="name1" placeholder="Name" required>
            </div>
            <div class="field">
                <input type="datetime-local" name="date1" placeholder="Date" step="300" required>
            </div>
            <div class="field">
                <input type="text" name="email1" placeholder="E-Mail">
            </div>
            <div class="field">
                <input type="tel" name="phone1" placeholder="Phone Number" required>
            </div>

            <div class="field">
                <textarea name="message1" placeholder="Message" requires></textarea>
            </div>

            <div class="g-recaptcha" data-sitekey="6LeKTjMUAAAAAJuZI0qqIBRp92slJoG4SESblWHw"></div>

            <input type="submit" name="reservation1" class="button" value="Send">

            <input type="hidden" name="hidden" value="1">
        </form>
    </div>

<?php 
    function lapizzeria_database(){
        global $wpdb;

        global $lapizzeria_db_version;
        $lapizzeria_db_version = "1.0";

        $table = $wpdb->prefix . 'reservations1';

        $charset_collate = $wpdb->get_charset_collate();

        // SQL Statement

        $sql = "CREATE TABLE $table ( 
                id mediumint(9) NOT NULL AUTO_INCREMENT, 
                name1 varchar(50) NOT NULL,
                date1 datetime NOT NULL,
                email1 varchar(50) DEFAULT '' NOT NULL,
                phone1 varchar(10) NOT NULL,
                message1 longtext NOT NULL,
                PRIMARY KEY (id)
        ) $charset_collate; ";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

    add_action('after_setup_theme', 'lapizzeria_database');

function lapizzeria_save_reservation() {

    if(isset($_POST['reservation1']) && $_POST['hidden'] == "1") {
              // read the value from recaptcha response
              $captcha = $_POST['g-recaptcha-response'];

              // Send the values to the server
              $fields = array(
                  'secret' => '6LeKTjMUAAAAAFeaj6Hq941AFvASw9sBJjeiCDyB',
                  'response' => $captcha,
                  'remoteip' => $_SERVER['REMOTE_ADDR']
              );

              // Start the request to the server

              $ch = curl_init('https://www.google/recaptcha/api/siteverify');

              // configure the request
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch, CURLOPT_TIMEOUT, 15);

              // Send the encode values in the URL 
              curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

              // Read the return value
              $response = json_decode(curl_exec($ch));
              if($response->success) {
                          global $wpdb;
                          $name = sanitize_text_field( $_POST['name1'] ) ;
                          $date = sanitize_text_field( $_POST['date1'] ) ;
                          $email = sanitize_email( $_POST['email1'] );
                          $phone = sanitize_text_field( $_POST['phone1'] ) ;
                          $message = sanitize_text_field( $_POST['message1'] ) ;

                          $table = $wpdb->prefix . 'reservations1';

                          $data = array(
                            'name1' => $name1,
                            'date1' => $date1,
                            'email1' => $email1,
                            'phone1' => $phone1,
                            'message1' => $message1
                          );

                          $format = array(
                            '%s',
                            '%s',
                            '%s',
                            '%s',
                            '%s'
                          );
                          $wpdb->insert($table, $data, $format );

                          $url = get_page_by_title('Thanks for your reservation!');
                          wp_redirect( get_permalink($url) );
                          exit();
              }


    }
}

add_action('init', 'lapizzeria_save_reservation');

 ?>

function lapizzeria_reservations() { ?>
  <div class="wrap">
      <h1>Reservations</h1>
      <table class="wp-list-table widefat striped">
          <thead>
              <tr>
                  <th class="manage-column">ID</th>
                  <th class="manage-column">Name</th>
                  <th class="manage-column">Date of Reservation</th>
                  <th class="manage-column">Email</th>
                  <th class="manage-column">Phone Number</th>
                  <th class="manage-column">Message</th>
                  <th class="manage-column">Delete</th>
              </tr>
          </thead>

          <tbody>
              <?php 
                global $wpdb;
                $table = $wpdb->prefix . 'reservations1';
                $reservations = $wpdb->get_results("SELECT * FROM $table", ARRAY_A); 
                foreach($reservations as $reservation): ?>
                    <tr>
                        <td><?php echo $reservation['id']; ?></td>
                        <td><?php echo $reservation['name1']; ?></td>
                        <td><?php echo $reservation['date1']; ?></td>
                        <td><?php echo $reservation['email1']; ?></td>
                        <td><?php echo $reservation['phone1']; ?></td>
                        <td><?php echo $reservation['message1']; ?></td>
                        <td>
                            <a href="#" class="remove_reservation" data-reservation="<?php echo $reservation1['id']; ?>">Remove</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
          </tbody>
      </table>
  </div>



<?php }


 ?>
Share Improve this question asked Mar 31, 2020 at 21:33 Faruk rızaFaruk rıza 982 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You have small mistake in your code. Use same variables in $data array and storing form data. Change your code to this:

$data = array(
    'name1' => $name,
    'date1' => $date,
    'email1' => $email,
    'phone1' => $phone,
    'message1' => $message
);

You have used $name at one place and $name1 at another place below.

Why Did @Sufyan Sheikh changed the code in his answer I dont know but his information is not true.The real code is in question post.

There is no problem with code.The problem is that phone number input value can be only 19 character.So, you must delete spaces in phone number and check it to is it 10 character or not.Its simple but works.

发布评论

评论列表(0)

  1. 暂无评论