0

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";
  }

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Hi FrankerZ, I removed the + symbol but it seems not working, like tablename and idEcht weren't posted
@user979974 Did you remove the = too?
True, I forgot to removed the = too. It works. Thanks so much FrankerZ

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.