user979974
user979974

Reputation: 953

PHP JQuery post 2 variables

I am trying to post 2 variables contained into 2 input type text with jQuery to a php page. I don't manage to retrieve data, I don't know what I am doing wrong. It works with one variable as soon as I put 2 it didn't.

my html page:

<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){

    var tablename = $('#tablename').val();  
    var idEcht = $('#idEcht').val();
              $.ajax({
        url: 'live_edit.php',
        type: 'POST',
        data: {'tablename=':+tablename, 'idEcht':+idEcht},
        dataType : 'html',
        success:function(data){
          $('p').html('the answer : ' +data);
        }
      });


});
    </script>
<head>
<body>
<input id='tablename' type='text' value='test'/>
<input id='idEcht' type='text' value='idEcht'/>

<p>
    </p>
</body>
</html>

the php script where I send data:

if(isset($_POST['tablename'],$_POST['idEcht'])) {
    $tablename = $_POST['tablename'];
    $idEcht = $_POST['idEcht'];
    echo $tablename;echo $idEcht;
  } else {
    echo "Noooooooo";
  }

Upvotes: 0

Views: 33

Answers (1)

Blue
Blue

Reputation: 22911

You don't need a + symbol. By putting the + symbol in front, you're actually trying to convert tablename and idEcht to a number, which returns NaN. Object notation in javascript is {key: value}. jQuery will simply take this object, and do all the magic to format/pass the data to the server:

{'tablename': tablename, 'idEcht': idEcht},

You can see how this works here:

$(document).ready(function(){
  var tablename = $('#tablename').val();  
  var idEcht = $('#idEcht').val();
  
  console.log({'tablename=':+tablename, 'idEcht':+idEcht});
  
  console.log({'tablename': tablename, 'idEcht': idEcht});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<input id='tablename' type='text' value='test'/>
<input id='idEcht' type='text' value='idEcht'/>

<p>
    </p>

Upvotes: 1

Related Questions