A
A
Aidar2015-11-23 00:14:19
PHP
Aidar, 2015-11-23 00:14:19

How to extract and send text content of checkbox to mail?

Hello! There is a form with checkboxes. I need to send the text content of the selected checkboxes to the mail.
Those. value is already taken by the price value. How to send text.... for example, you have selected the "House" checkbox with the value 4000000. You need to send the word "House".

<input type="checkbox" name="chk1" value = "4000000">Дом
 <input type="checkbox" name="chk1" value = "1000000">Авто
 <input type="checkbox" name="chk1" value = "25000">Телефон


Thanks in advance.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Dyrkov, 2015-11-23
@VIKINGVyksa

Well, when you render your boxes, you can specify something like this

<input type="checkbox" name="chk1" value = "4000000{//}Дом">Дом
 <input type="checkbox" name="chk1" value = "1000000{//}Авто">Авто
 <input type="checkbox" name="chk1" value = "25000{//Телефон}">Телефон

Those. add through a unique delimiter to value. In php it's just to add one more concatenation, I think you get the idea. When the value arrives at the server, you will only have to split the value string by the {//} or : character, as you prefer. Then the first value will be a digit and the other a string.
If you use AJAX, then you can create an object in which everything will be, there should be no problems)

I
Ivan Volkov, 2015-11-23
@oOLokiOo

HTML:

...

<head>
  <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#ajax_form").on("submit", function(){
        var data = [];
        
        $("#ajax_form input[type='checkbox']:checked").each(function(){
          data.push({value: $(this).val(), text: $("label[for="+$(this).attr("id")+"]").text()});
        });

        $.ajax({
          type: "POST",
          url: "test.php?do_action=some_action3",
          data: {chk1: data},
          success: function(sum){
            alert("Сумма - "+sum);
          }
        });

        return false;
      });
    });
  </script>
</head>

...

  <main>
    <section>
      <form method="post" action="">
        <input type="hidden" name="do_action" value="some_action" />
        <label for="house"><input type="checkbox" name="chk1[Дом]" id="house" value="4000000" />Дом</label>
        <label for="avto"><input type="checkbox" name="chk1[Авто]" id="avto" value="1000000" />Авто</label>
        <label for="phone"><input type="checkbox" name="chk1[Телефон]" id="phone" value="25000" />Телефон</label>
        <button type="submit">go! go! go!</button>
      </form>
    </section>
    <p> или например так:</p>
    <section>
      <form method="post" action="">
        <input type="hidden" name="do_action" value="some_action2" />

        <label for="house2"><input type="checkbox" name="chk1[0]" id="house2" value="4000000" />Дом</label>
        <input type="hidden" name="chk1_value[0]" value="Дом" />
        <label for="avto2"><input type="checkbox" name="chk1[1]" id="avto2" value="1000000" />Авто</label>
        <input type="hidden" name="chk1_value[1]" value="Авто" />
        <label for="phone2"><input type="checkbox" name="chk1[2]" id="phone2" value="25000" />Телефон</label>
        <input type="hidden" name="chk1_value[2]" value="Телефон" />

        <button type="submit">go! go! go!</button>
      </form>
    </section>
    <p>ну или с AJAX, вот так:</p>
    <section>
      <form method="post" action="" id="ajax_form">
        <input type="hidden" name="do_action" value="some_action3" />
        <label for="house3"><input type="checkbox" name="chk1[]" id="house3" value="4000000" />Дом</label>
        <label for="avto3"><input type="checkbox" name="chk1[]" id="avto3" value="1000000" />Авто</label>
        <label for="phone3"><input type="checkbox" name="chk1[]" id="phone3" value="25000" />Телефон</label>
        <button type="submit">go! go! go!</button>
      </form>
    </section>
  </main>

if (isset($_REQUEST['do_action'])) {
  switch ($_REQUEST['do_action']) {
    case 'some_action':
      foreach ($_REQUEST['chk1'] as $k => $v) {
        echo $k.' - '.$v.'<br />';
      }

      $sum = array_sum($_REQUEST['chk1']);
      echo '<br />Сумма - '.$sum;
      break;

    case 'some_action2':
      foreach ($_REQUEST['chk1'] as $k => $v) {
        echo $_REQUEST['chk1_value'][$k].' - '.$v.'<br />';
      }

      $sum = array_sum($_REQUEST['chk1']);
      echo '<br />Сумма - '.$sum;
      break;

    case 'some_action3':
      $sum = 0;
      for ($i = 0; $i < count($_REQUEST['chk1']); $i++) {
        //echo $_REQUEST['chk1'][$i]['text']; // do something with text here!
        $sum += $_REQUEST['chk1'][$i]['value'];
      }
      echo $sum;
      
      exit();
      break;
  }
}

S
Stalker_RED, 2015-11-23
@Stalker_RED

This is exactly what the name attribute is intended for, which for some reason is the same on all three checkboxes.

G
GoldenYear, 2015-11-23
@GoldenYear

The checkbox does not imply the selection of one element from several. Therefore, if there are several checkboxes in one form, then their name must be different !

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question