PDA

View Full Version : javascript variable to php



Bicklo
10-15-2017, 10:08 AM
Hello all, I have a variable in javascript and I want to use it in php can anyone help me out here?

This is the javascript and variableToSend is the one I like to use in php.



<script type="text/javascript">

$('div.bekijkitem').click(function() {
var text = $(this).text();

var variableToSend = text;
$.post('meebekijken.php', {variable: variableToSend});


alert (variableToSend);

});

</script>

jscheuer1
10-15-2017, 04:06 PM
That should do it. The value of variableToSend should be posted to meebekijken.php as the value $_POST['variable'] - what's the problem?

Bicklo
10-15-2017, 08:02 PM
That should do it. The value of variableToSend should be posted to meebekijken.php as the value $_POST['variable'] - what's the problem?

It does not show if I try



echo $_POST['variable'];


or do I miss something.

Here is the full page



<?php

session_start();

include ("dbase/verbinding.php");


$naams = $_SESSION['paswoord'];


$sql= "SELECT paswoord FROM mails WHERE mail = '$naams'";
$stmt = $db->prepare($sql);
$stmt->execute();
$obj = $stmt->fetchObject();

?>





<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>


<title></title>
<meta name=”description” content=”Edri”/>

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="iecss.css" />
<![endif]-->





<!-------- Css media-queries width -------->


<link rel="stylesheet" href="css/style.css">

<link rel='stylesheet' media='screen and (max-width: 1200px)' href='css/1260.css' />


<link rel='stylesheet' media='screen and (max-width: 1090px)' href='css/1040.css' />


<link rel='stylesheet' media='screen and (max-width: 910px)' href='css/910.css' />


<link rel='stylesheet' media='screen and (max-width: 820px)' href='css/820.css' />


<link rel='stylesheet' media='screen and (max-width: 760px)' href='css/760.css' />


<link rel='stylesheet' media='screen and (max-width: 640px)' href='css/570.css' />


<link rel='stylesheet' media='screen and (max-width: 470px)' href='css/450.css' />

<link rel='stylesheet' media='screen and (max-width: 370px)' href='css/310.css' />



<!-------- Jquery -------->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


<script type="text/javascript" src="js/main2.js"></script>



</head>
<body>


<div id="navigeren">


<div id="terug"><div id="navtekstl"><a href='main.php'><div id="menuitem">Start</div></a></div></div>

<div id="weer"><div id="navtekstl"><a href='main.php'><div id="menuitem">Start</div></a></div></a></div>



<script type="text/javascript">


var myvar='<?php echo $_SESSION['paswoord']; ?>';



if (myvar == 'steven.lapauw@vzweffect.be'){

$('#terug').hide();
$('#weer').show();

}else{

$('#terug').show();
$('#weer').hide();

};


</script>



</div>


<div id="pasverander">

<div id="gebruiker">
<?php echo $_SESSION['paswoord']; ?>
</div>


<form action="" method="POST">

<h1 class="origitext">Huidig paswoord:</h1>

<input type="text" id="origipas" name="origipasa"/>

<?php



?>


<h1 class="newtexta">Nieuw paswoord:</h1>

<input type="text" id="newpasa" name="newpasaa"/>




<h1 class="newtextb">Herhaal nieuw paswoord:</h1>

<input type="text" id="newpasb" name="newpasbb"/>

<?php

echo $obj->paswoord;
echo $naams;
?>


<button class="pasbutton" type="submit">Paswoord bevestigen</button>


</form>


</div>


</body>

</html>


Thank you.

jscheuer1
10-16-2017, 01:43 PM
Why would it show with:


echo $_POST['variable'];

? You're not loading meebekijken.php and (in your jQuery $.post code) you've not done anything with any response from it. I have no idea what meebekijken.php does or what you expect it to do, but it can only act on the server side, at least when called as part of a jQuery $.post command with no response. It's possible the value isn't getting through, but it should be unless you have a typo or glitch there I've missed.

I'm curious what all that other code* you included in your post is supposed to be. It doesn't have any references to any of the code you or I have mentioned up until that point. At least none that I can see.

*
<?php

session_start();

include ("dbase/verbind . . .

Bicklo
10-16-2017, 03:04 PM
I am sorry this is not the right page,

this is the right page,




<body>

<div id="meebekijken">


<div id="bekijkreeks">
<?php

foreach($db->query('SELECT mail FROM mails') as $row) {


echo'<a href="#" align-left = 10px;><div class="bekijkitem">'.$row['mail'].'</div></a>';

}

?>

<script type="text/javascript">

$('div.bekijkitem').click(function() {
var text = $(this).text();

var variableToSend = text;
$.post('meebekijken.php', {variable: variableToSend});


alert (variableToSend);

});

</script>


<?php




?>


</div>


<div id="inforeeks">


<div id="infolijn">


<div id="infonaamtext">Naam :</div>

<div id="infonaam">
&ensp;
<?php

echo $_POST['variableToSend'];


?>

</div>

</div>


<div id="infolijn">

<div id="infonaamtext">Functie :</div>


<div id="infofunctie">
&ensp; functie
</div>

</div>



<div id="infolijn">

<div id="infonaamtext">Tel :</div>


<div id="infofunctie">
&ensp; telefoon
</div>

</div>




</div>



</div>






</body>


I need the variableToSend variable in php...

jscheuer1
10-16-2017, 03:57 PM
OK, well this:


<script type="text/javascript">

$('div.bekijkitem').click(function() {
var text = $(this).text();

var variableToSend = text;
$.post('meebekijken.php', {variable: variableToSend});


alert (variableToSend);

});

</script>

Does nothing until someone clicks on one of the bekijkitem div elements. So anything it does cannot show up on this page unless it's via javascript alone, or unless the page is reloaded after it happens. I don't know what's on meebekijken.php, but it should receive the value. It cannot communicate back to this page though unless you use it's response from the AJAX call. It can set values in the session (perhaps), or drop a PHP cookie (more likely), and certainly can enter a value into a flat file or a database. Once it does one of those things, that value will be available in future PHP page loads and future PHP script runs.

To make this a little clearer, when you do:


$.post('meebekijken.php', {variable: variableToSend});

all you are doing is running the code on meebekijken.php with the POST value of 'variable' set to whatever variableToSend was at that moment. It doesn't change the anything on this page. Further, only the server side code on meebekijken.php will do anything. Any HTML or javascript on that page will be ignored in the current setup. If you were to do something with the response from meebekijken.php*, then both server side code and text/HTML on meebekijken.php could do something, but any javascript would still be ignored, as the page isn't loaded into the browser, just requested from the server.


*By having a return function for the $.post request, something like:


$.post('meebekijken.php', {variable: variableToSend}, function(data){alert(data);});

Then if you got anything back from meebekijken.php, you could use it right away via javascript on the current page.

Usually this is what's done. We send send data out to a PHP script, it can set something in a database or elsewhere for later use in PHP, and can also send the same information, or even something else (like a confirmation) back to the current page for immediate use by javascript on the current page.

jscheuer1
10-16-2017, 04:57 PM
See also my previous post. I've made you up a basic working example. Put both files in a separate folder and play around with them (only load s.php into the browser):

s.php (initial sending page):


<?php
session_start();
if(isset($_SESSION['var'])){
echo $_SESSION['var'];
}
?>
<div>Some Value</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$('div').click(function(){
var text = $(this).text();
$.post('r.php', {var: text}, function(data){alert(data);});
});
</script>

r.php (initial receiving page):


<?php
if(isset($_POST['var'])){
session_start();
$_SESSION["var"] = $_POST['var'];
echo $_SESSION["var"];
}
?>

After you send the data by clicking on the div, it should alert the value. If you then refresh the page, it should print the value (show it twice).

Bicklo
10-17-2017, 05:14 AM
Thank you sir, I will have a look at this later today.