Results 1 to 8 of 8

Thread: javascript variable to php

  1. #1
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default javascript variable to php

    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.

    Code:
    <script type="text/javascript">
    
    $('div.bekijkitem').click(function() {
        var text = $(this).text();
        
    	var variableToSend = text;
    $.post('meebekijken.php', {variable: variableToSend});
    
    	
    	alert (variableToSend);
    	
    });
    
    </script>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,371
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    That should do it. The value of variableToSend should be posted to meebekijken.php as the value $_POST['variable'] - what's the problem?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    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

    Code:
    echo $_POST['variable'];
    or do I miss something.

    Here is the full page

    Code:
    <?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.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,371
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    Why would it show with:

    Code:
    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.

    *
    Code:
    <?php
    
    session_start();
    
    include ("dbase/verbind . . .
    Last edited by jscheuer1; 10-16-2017 at 03:32 PM. Reason: typo
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    I am sorry this is not the right page,

    this is the right page,


    Code:
     <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...

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,371
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    OK, well this:

    Code:
    <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:

    Code:
    $.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:

    Code:
    $.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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,371
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    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 Code:
    <?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 Code:
    <?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).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Thank you sir, I will have a look at this later today.

Similar Threads

  1. Replies: 7
    Last Post: 06-13-2011, 07:18 AM
  2. Using Javascript variable in src tag?
    By HooliganR6 in forum JavaScript
    Replies: 2
    Last Post: 10-18-2006, 06:45 PM
  3. ASP variable with javascript
    By joecool2005 in forum JavaScript
    Replies: 0
    Last Post: 05-25-2006, 06:06 PM
  4. PHP variable in JavaScript
    By Jon Weir in forum PHP
    Replies: 0
    Last Post: 03-30-2006, 12:44 PM
  5. Requesting a javascript variable
    By Eclyps19 in forum ASP
    Replies: 2
    Last Post: 02-27-2006, 08:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •