PHP JQuery post 2 variables

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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




1 Answer
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


idEcht


key: value


'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>





Hi FrankerZ, I removed the + symbol but it seems not working, like tablename and idEcht weren't posted
– user979974
Aug 8 at 7:38





@user979974 Did you remove the = too?
– FrankerZ
Aug 8 at 7:39


=





True, I forgot to removed the = too. It works. Thanks so much FrankerZ
– user979974
Aug 8 at 7:41






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS