-1

I am doing a dynamicall table such as:

 <form method="POST" action="recieve.php">
  <table>
   <thead>
    <th>Value</th>
    <th>Action</th>
   </thead>
   <tbody>
    <tr>
     <td><input type="hidden" name="value_original[0]" id="value_original[0]" value="20000">
       <input type="text" id="value[0]" name="value[0]"></td>
     <td><button type="button" class="btn btn-info add_input_button" title="Add" onclick="addFunction()"></td>
     </tr>
    </tbody>
   </table>
  </form>
  <button id="send" type="submit" class="btn btn-success" onclick="check()">Aceptar</button>

The add and delete field are working fine giving me extra spaces in case I need them but after I hit the send button I need it to check if the value is ok, otherwise I need it to ask for a special permission to send it.

 <script>
   var value_original = [];
   var value = [];
   function check(){
    var j=0;
    var k=0;
    for(j;j<=i;j++){
     value_original[j] = document.getElementById('value_original['+j+']').value;
     value[j] = document.getElementById('value['+j+']').value;
     value_original[j]=Number(value_original[j]);
     value[j]=Number(value[j]);
     if(value[j]<value_original[j]){
      k++;
     }
    }
    if(k>0){
    //ask permission
     return false;
    }
    else{
     return true;
    }
   }
  </script>

I have the other php with the function add($data,$username,$password), where username and password are defined in the permission pop up using sweetalert2, which is currently working, but I need to send that information to recieve.php.

How can I do that?

2 Answers2

0

Instead of returning true submit the form in else part, like this :

if(k>0){
//ask permission
 return false;
}
else{
 $('form').submit();
}
Santu Roy
  • 197
  • 7
0

Another way would be using Ajax:

     $.ajax({
        url: 'http://localhost/recieve.php?user=Me&pass=1234&data=whatever',
        dataType: 'json',
        async: true,
        success: function (PhpResponse)
        {              
           if (PhpResponse !== null) {                
              console.log(PhpResponse);                                         
           }
        }
     });

The PHP should be able to get and process this parameter, something like this:

if (isset($_GET["user"]) && isset($_GET["pass"])  && isset($_GET["data"])) {    
   $GLOBALS['response'] = 'I got the data!!';
}
Jhollman
  • 2,093
  • 24
  • 19
  • If I use this, will I recive all the data on the other side? Including the text inputs in the table? – Ricardo Javier Chavarra Gonzal Oct 25 '18 at 21:00
  • yes you can, all u have to do is to assemble the data u wish to send into a JSON string then pass it to the AJAX call in the 'data' argument. check this post: https://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and-receive-json-response-from-controller-mvc – Jhollman Oct 26 '18 at 22:47