-1

I have a checkbox and I need to get data (id) from them and send it to other php file

My code :

function getValues() {
var str = [];
var checks = document.getElementsByClassName('justtest');
          for(var i=0; i<checks.length; i++){
              if(checks[i].checked){
                str.push( checks[i].id );
              }
            }

  for( var x = 0; x < str.length; x++){
    alert (str[x]);
    }

  }
<div class="form-group">
   <div class="col-md-3">
      <div class="checkbox">
         <label>
<input type="checkbox" value="test1" class="justtest" id="1"> Test1
         </label>
       </div>
      </div>
     </div>
    <div class="form-group">
    <div class="col-md-3">
     <div class="checkbox">
       <label>
 <input type="checkbox" value="test2" class="justtest" id="2"> test2
       </label>
      </div>
     </div>
    </div>
     <div class="form-group">
      <div class="col-md-3">
       <div class="checkbox">
         <label>
<input type="checkbox" value="test3" class="justtest" id="3"> test3
          </label>
         </div>
        <button onclick="getValues()">Check</button>
       </div>
      </div>

So, I need to take Id from checkbox and send it to my PHP file to show result about ID

function getValues() {
var str = [];
var checks = document.getElementsByClassName('justtest');
          for(var i=0; i<checks.length; i++){
              if(checks[i].checked){
                str.push( checks[i].id );
              }
            }

  for( var x = 0; x < str.length; x++){



  $.ajax({
    url: 'result.php',
    cache: false,
    data: str[x],
    type: 'POST',

                   });
                     }
  }

I have other php file named result.php, So, I need to take ID's from this file and send it to result.php

they don't work

Any solution ?

saadsaad
  • 35
  • 12
  • 1
    Depends on how that "other PHP file" is being invoked. Are you looking to make an AJAX request? POST a form to the other page? Something else? – David Oct 18 '16 at 13:42
  • I have other php file named `result.php`, So, I need to take ID's from this file and send it to `result.php` – saadsaad Oct 18 '16 at 13:43
  • 1
    Please do some research before posting. There are various ways from form posts to ajax that can achieve what you are looking for. A simple search on this site of "javascript send php variable" presents quite the list. – scrappedcola Oct 18 '16 at 13:43
  • Possible duplicate of [jQuery Ajax POST example with PHP](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – scrappedcola Oct 18 '16 at 13:43
  • @scrappedcola, they send a text value, I need to send a ID from checkbox – saadsaad Oct 18 '16 at 13:44
  • 1
    @saadsaad: You can send any value you like. Nobody here is going to write a complete custom-made solution just for you. You need to specify what you're trying to accomplish (you haven't) and where you're stuck (you haven't tried anything). – David Oct 18 '16 at 13:45
  • @David I try a lot but without result, for this I create a post here – saadsaad Oct 18 '16 at 13:46
  • 1
    @saadsaad: Your question contains no attempt to solve the problem, nor any necessary specifics on what the problem even is. If you're just asking how to use JavaScript and PHP, there are many tutorials available online for that. If you have a specific question, please elaborate. – David Oct 18 '16 at 13:49
  • @David I need to send data that I take it from checkbox with javascript ( I find the solution about it ) the second and last think Is the send this data to my PHP file, the problem is first I have a table and I need to send the ID not the value – saadsaad Oct 18 '16 at 13:52
  • @saadsaad: Then the linked duplicate question in a previous comment will help you. Just replace the "serialized data" with the information you want to send in the AJAX request. Note that we're *assuming* that you want to do this via AJAX, because you aren't specifying otherwise. If you make an attempt and actually get stuck somewhere, we can help with that. If you just want someone to show you how JavaScript and PHP work, hire a tutor. – David Oct 18 '16 at 13:55
  • @David wait I give you my try – saadsaad Oct 18 '16 at 13:57
  • @David the second problem, my input is not in a form, is that make me a problem ? – saadsaad Oct 18 '16 at 14:00
  • @David I edit my post – saadsaad Oct 18 '16 at 14:13

1 Answers1

0

For starters, you have a syntax error that is undoubtedly being reported by your browser console:

$.ajax({
         url: 'result.php',
         cache: false,
         data: str[x],
         type: 'POST' // <--- You had an extra comma
}); // <--- You forgot to close the object and the function call

Aside from that, what is in str[x]? Form data would need to be in the form of key/value pairs or a JSON object. If str[x] is just a single value then you'd need to provide a key for that value. Something like this:

data: 'someKey=' + str[x]

(If the value is complex, you might enclose it in encodeURIComponent() to URL-encode it.)

Then in the server-side code you would access the value using that key:

$_POST['someKey']
David
  • 208,112
  • 36
  • 198
  • 279
  • I don't know why it's not work ? I need to make it under the if ? where's the mistake in my code – saadsaad Oct 18 '16 at 14:21
  • @saadsaad: You're going to have to be more specific than that. Try to understand that I can't see your screen from here. What is the current state of your code and how specifically is it failing? When you debug it, where exactly does the problem occur and what is the problem? – David Oct 18 '16 at 14:23
  • I think we don't need ajax, because ajax need a reponse, but me I need to show other page. My problems : 1) I have a checkbox with IDs, the IDs is just a numbers 2) I need to take IDs where is checked (It's work for me) 3) Send the IDs to the other file and search in DATABASE where the IDs = ID project – saadsaad Oct 18 '16 at 14:32
  • @saadsaad: What does "AJAX needs a response" mean? You can respond to the AJAX operation or not respond to it, it's entirely up to you. If you need to navigate the user to another page, you can do so from the AJAX response handler. Or you could not use AJAX and just make this a standard form POST. This was asked in the first comment on the question above, and you've yet to provide any clarification on what you're attempting to do. Nobody here is going to provide you with a complete and working solution that meets all of the needs you haven't even told us. You need to put in *some* effort. – David Oct 18 '16 at 14:35
  • If I have form it's so easy for me, the problem, I have just INPUTs I don't have form – saadsaad Oct 18 '16 at 14:36
  • @saadsaad: Then wrap the inputs in a `
    ` element.
    – David Oct 18 '16 at 14:37
  • I try to use `windows.location.href` – saadsaad Oct 18 '16 at 14:45
  • 1
    @saadsaad: The JavaScript console is probably telling you that `windows` is undefined. The term you're looking for is `window`. You should really look at the browser's development console when you're developing. – David Oct 18 '16 at 15:03