-2

Below is the code I have.

The flow needs to be:

-On button click, value is passed to JS

-JS sets as variable

-The variable needs to be sent to PHP without refreshing the page (AJAX?)

-The variable needs to be output in php

Can anyone show me how to do this?

<a href="#modal-accept" data-toggle="modal"><button onclick="updateVar(2)" class="btn btn-acceptColor m-t-lg">Accept</button></a>
<script type="text/javascript">
function updateVar(profferRequestID){
var profferRequestID;
//pass to ajax then pass to php??
}
</script>     

<?php
//echo out that JS variable
echo $profferRequestID; 
?>
Alex
  • 9
  • 5

3 Answers3

0

try something like this

using jquery

javascript code

var name = "John";
$.get( "your_file.php", { user_name: name},function( data ) {
    // on success perform action you want.
});

php code

$user_name = $_GET['user_name'];

ANOTHER WAY In javascript (not checked)

var name = "John";
var xmlhttp;
if (window.XMLHttpRequest)
{
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
         // on success perform action you want.
    }
    }
xmlhttp.open("GET","your_file.php?user_name="+name,true);
xmlhttp.send();

EDITED CODE

function updateVar(val){
    $.get( "path_to/your_file.php", { profferRequestID : val},function( data ) {
        // on success perform action you want.
    });
}

your_file.php

 $profferRequestID = $_GET['profferRequestID'];
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
  • 1
    Don't forget to mention the poster needs jQuery with your code. – Jaap Moolenaar Dec 24 '13 at 06:15
  • can you explain this line $.get( "your_file.php", { user_name: name},function( data ) ? or better, answer using the code i posted in question as I can't understand what your doing. e.g. "your_file.php", whats that? I don't need other files. – Alex Dec 24 '13 at 06:19
  • you can check here http://api.jquery.com/jquery.get/ – rajesh kakawat Dec 24 '13 at 06:21
  • @rajeshkakawat I think what your giving me is for something else, I can't see its relevance to this question – Alex Dec 24 '13 at 06:22
  • i have given you a way to pass js variable to php without page refresh ie AJAX – rajesh kakawat Dec 24 '13 at 06:24
  • can you show me how in relation to the code I actually posted please? that way it will make sense to me. thanks – Alex Dec 24 '13 at 06:25
  • In above sample code you can pass var name value to your_file.php without refreshing page – rajesh kakawat Dec 24 '13 at 06:25
  • is your_file the file that this code goes in? Can you show me using the code I put in the question – Alex Dec 24 '13 at 06:30
  • doing this: doesn't work for me – Alex Dec 24 '13 at 06:34
  • @rajeshkakawat none of these ways work – Alex Dec 24 '13 at 12:44
0

Oy. I need 6 more points before I can add a comment.

Please answer the following questions:

  1. Does this have to be cross-browser compatible?
    • If so, you may want to consider using the jQuery JavaScript library. There are a "ton" of browser inconsistencies with AJAX that jQuery abstracts out.
    • Either way, you may consider using jQuery because of it's powerful AJAX libraries.
  2. Do you want to return the php response to your JavaScript function, or display it somewhere on your HTML page?

After you answer that, I'll be happy to provide you with a complete answer. I'm trying to give back to the StackOverflow community. :)

Nostalg.io
  • 3,662
  • 1
  • 28
  • 31
  • 1. of course 2. no, as per the question says - display it in PHP – Alex Dec 24 '13 at 06:21
  • I'm not sure what you mean by "display it in PHP". PHP is a server side programming language. Unless you return the PHP response to the AJAX script, and then manipulate the webpage code to display it, it won't be displayed anywhere except maybe the server logs. – Nostalg.io Dec 24 '13 at 06:52
-1

I'm not familiar with AJAX, or PHP, but I know that in javascript, you can read/edit innerhtml. I think your best bet would be to create a hidden element, and use that to store your variables.

Derpster13
  • 115
  • 1
  • 1
  • 7
  • that wouldn't work. if i put a variable into JS how do I get it out in PHP without refreshing? – Alex Dec 24 '13 at 06:17