PDA

View Full Version : Sending javascript variable to php page



Bicklo
12-08-2016, 07:29 PM
I encountered a new problem which I can't resolve after looking at 100+ topics on the net.

I want to send a variable to a php page and then open it.

Any tips?

javascsript



<script type="text/javascript">

$(document).on("click",".namen", function(e) {

var woon = $(this).text();

$('#uitkomst').load('member.php');


$.post('member.php', {variable: woon});



e.preventDefault();


});

</script>


member.php



<?php
echo variable;
?>


I work with a local server xampp, could there be a problem?

jscheuer1
12-09-2016, 03:35 AM
There are many ways to do this, here's one (sending/requesting page):


<!DOCTYPE html>
<html>
<head>
<title>Sending/Requesting</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).on('click', '.namen', function(e){
var woon = $(this).text();
$.ajax({
type: 'post',
url: 'member.php',
data: {variable: woon}
}).then(function(data){$('#uitkomst').html(data);});
e.preventDefault();
});
</script>
</head>
<body>
<a href="#" class="namen">Namen</a><br>
<a href="#" class="namen">Something Else</a>
<div id="uitkomst"></div>
</body>
</html>

And on member.php:


<?php
if(isset($_POST['variable'])){
echo $_POST['variable'];
}
?>

But, if that's all you want to do, there's no need for AJAX or for an external page.

Bicklo
12-09-2016, 06:06 AM
Thank you sir, you did it again.

It looks like I was close a few times but not close enough.

Thank you so much.