1

I would like to pass two array data to php and extract them in two prefix so I can choose them by that two prefix.

Example code

HTML

<form>
  <!-- set one -->
  <div class="one">
    <input id="name" name="name">
  </div>
  <!-- /set one -->

  <!-- set two -->
  <div class="two">
    <input id="email" name="email">
  </div>
  <!-- /set two -->

  <!-- set one again -->
  <div class="one">
    <input id="msg" name="msg">
  </div>
  <!-- /set one -->

  <button type="submit"></button>
</form>

Jquery

I don't know how to pass set one and set two in data, should it pass by two array? I would like to select them by parent class one and two.

  $('form').on('submit', function(e) {
    e.preventDefault();

  $.ajax({
      type: 'POST',
      url: 'contact.php',
      data: $('form').serialize(),
      success: function(data) {
        console.log(data);
      },
      error: function(data) {
        console.log(data);
      }
    });
  });

PHP

<?php
  extract($_POST, EXTR_PREFIX_ALL, 'one'); // how to extract only set one?
  extract($_POST, EXTR_PREFIX_ALL, 'two'); // how to extract only set two?
>

I want to use them like this in php:

if (empty($one_name) || empty($two_email) || empty($one_msg)) { // select them by prefix
  // function
}

The html can not include php code, how can use jquery and php do that?

Thanks.

2 Answers 2

2

Use array names on the form controls

<div class="one">
    <input id="name_1" name="one[name]">
  </div>
  <!-- /set one -->

  <!-- set two -->
  <div class="two">
    <input id="email_2" name="two[email]">
  </div>

 <!-- set one again -->
  <div class="one">
    <input id="msg" name="one[msg]">
  </div>

Your serialize() method needs no changes

Receive in php

$data_one  = $_POST['one'];    
echo $data_one['name'];
Sign up to request clarification or add additional context in comments.

Comments

1

This is far more simplistic than Charlie's answer, but you can also do this -- if you can avoid using .serialize()

$('form').on('submit', function(e) {
    e.preventDefault();

    var one_name = $('.one #name').val();
    var two_email = $('.two #email').val();
    var one_msg = $('.one #msg').val();

    $.ajax({
        type: 'POST',
        url: 'contact.php',
        data: 'one_name=' +one_name+ '&two_email=' +two_email+ '&one_msg=' +one_msg,
        success: function(data) {
            console.log(data);
        },
        error: function(data) {
            console.log(data);
        }
    });
});

PHP:

<?php
    $one_name = $_POST['one_name'];
    $two_email = $_POST['two_email'];
    $one_msg = $_POST['one_msg'];

2 Comments

why would you want to avoid serialize(). No fun building long strings in a big form .. or building big objects either
Totally agree. Just that sometimes beginners don't know all the options. I don't know the OP, probably he already knows the above, but perhaps he does not. Just fwiw. LOVE your answer, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.