• Resolved helvetica123

    (@helvetica123)


    When I add data to database manuelly, it shows it on admin panel but the form doesnt insert inputs t? database.

    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

    
    <!-- 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' => 'xxxxxx my API key here xxxxxxx',
                      'response' => $captcha,
                      'remoteip' => $_SERVER['REMOTE_ADDR']
                  );
    
                  // Start the request to the server
    
                  $ch = curl_init('https://www.google.com/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 }
    
     ?>
    • This topic was modified 4 years, 11 months ago by bcworkz. Reason: obfuscate API key
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    So, just a question. Are you developing a plugin? If not, why not use one that does all this for you, like formidable?

    Thread Starter helvetica123

    (@helvetica123)

    @sterndata yeah, I am developer.

    Moderator bcworkz

    (@bcworkz)

    As Steve suggests, a form builder plugin can save you a lot of heartburn.

    If you posted your actual API key in the code, I recommend you generate a new one. I’ve obfuscated it just now, but it may have already been scraped for bad actors to utilize.

    There’s nothing obvious indicating why your code isn’t adding data. You’ll need to do your own debugging. Be sure that everything your code does yields the result you expect. For example, error_log() intermediate values to confirm everything does what it is supposed to. As a guess, look closely at the response from Google, IME it’s frequently not what we expect.

    BTW, your form scheme ought to set and verify a nonce to ensure requests really come from a recently served version of your form. It’s more secure than $_POST['hidden'] == "1"

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Form doesnt save to database (insertion) -wordpress’ is closed to new replies.