6 Replies - 290 Views - Last Post: 16 October 2017 - 09:25 AM

#1 Bicklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 23-November 16

javascript variable to php

Posted 15 October 2017 - 03:06 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>



Is This A Good Question/Topic? 0
  • +

Replies To: javascript variable to php

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: javascript variable to php

Posted 15 October 2017 - 03:09 AM

It looks like you are already posting it to php. What php have you written in the page that receives the value?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 13,474
  • Joined: 08-August 08

Re: javascript variable to php

Posted 15 October 2017 - 03:57 AM

It looks to me like you're sending an object with no name that contains a variable called variable which contains the value of variableToSend. Why not just send text or $(this).text()?
Shouldn't try to debug just after waking...
I'm a little clearer now, so I'm thinking it needs to be an array, not an object: [variable: text]
ugh, it's fine the way it is. Going back to sleep!

This post has been edited by CTphpnwb: 15 October 2017 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 Bicklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 23-November 16

Re: javascript variable to php

Posted 15 October 2017 - 04:28 AM

View Postandrewsw, on 15 October 2017 - 03:09 AM, said:

It looks like you are already posting it to php. What php have you written in the page that receives the value?



Just tried with echo, alert (variableToSend) gives me what I need and now I just have to use this text in php.

View PostCTphpnwb, on 15 October 2017 - 03:57 AM, said:

It looks to me like you're sending an object with no name that contains a variable called variable which contains the value of variableToSend. Why not just send text or $(this).text()?
Shouldn't try to debug just after waking...
I'm a little clearer now, so I'm thinking it needs to be an array, not an object: [variable: text]


The text is an email adres that i need in php.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: javascript variable to php

Posted 15 October 2017 - 04:34 AM

Again, what php have you tried? How much php do you understand?
Was This Post Helpful? 0
  • +
  • -

#6 Bicklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 23-November 16

Re: javascript variable to php

Posted 15 October 2017 - 05:50 AM

Here's the full 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 

echo variableToSend;

?> 


 </div>
 
 
  <div id="inforeeks">
  
  
  <div id="infolijn">

  
  <div id="infonaamtext">Naam :</div>
  
  <div id="infonaam">
&ensp; 

<?php 
 
 $variable = $_POST['variable'];
  
  echo $variable;
 
 ?>

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


Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1830
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: javascript variable to php

Posted 16 October 2017 - 09:25 AM

That HTML is kind of a mess, but it looks like you're getting the value on line 57. The code on line 36 is just an error in PHP, you're trying to echo an undefined constant in PHP.

Just so you understand, you can't have a single browser request where you use PHP to produce a page and somehow simultaneously have Javascript send a value to PHP to be used later in the same PHP code. That requires multiple browser requests. What you can do is have PHP produce your first page, and then use Javascript to send the value via ajax like you're already doing, but the page that you're sending the value to via ajax should only do whatever PHP processing is necessary, return a response to Javascript, and then you can have an ajax callback function update the existing HTML page with the response from PHP. I'm not really sure what you're trying to accomplish in the above code with sending the email address to PHP, what exactly do you do with it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1