Asked  2 Years ago    Answers:  5   Viewed   80 times

First I thought that I had to convert JavaScript to PHP, but then I found out that I cannot because of server and client side executions. So now I simply want to send ONE variable

<script type="text/javascript">
function scriptvariable()
{        
    var theContents = "the variable";
}
</script>

to a PHP variable

<?php
$phpvariable
?>

That function in the JavaScript executes when let's say I click on a button.

Now I have no idea on how to assign that phpvariable to the JavaScript one to use the phpvariable to look up stuff in my database. I know I can add it to my url or some thing and refresh the page, but I would like to do it with AJAX as I might have to use this Ajax method further in my webpage.

So is there an easy way to do this without having to dump pages of code on my page to do one simple thing?

 Answers

4

As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post

Maybe the most easiest approach for you is something like this

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "myphpfile.php?name=" + javascriptVariable; 
}

On your myphpfile.php you can use $_GET['name'] after your javascript was executed.

Regards

Friday, September 30, 2022
2
<script>
   $.ajax({
      type: "POST",
      url: "index.php",
      data: {var:'value'},
      dataType: 'text',
      success:function(data){
       // Test what is returned from the server
         alert(data);
      }
    });
 </script>
Thursday, August 25, 2022
1

Your quick solution would be:

$("#select_coffee_talk_year").button().click(function() {
    var form = $('#coffee_talk_year');  
    var data = form.serialize();

    $.ajax({
        url: "include/scripts/select_event.php",
        type: "POST",
        data: data,
        dataType: 'json',
        success: function (select) {
            var coffee_talk = $("#coffee_talk");
            coffee_talk.fadeOut('fast', function() {
                for(i in select) {
                    row = select[i];
                    div = coffee_talk.append('<div id="row_'+i+'" />');
                    for(column in row) {
                       div.append('<span class="column_'+column+'">'+row[column]+'</span>');
                    }
                }
                coffee_talk.fadeIn();
            });
        }   
    });
    return false;
});

For a nicer approach you should lookup Moustache.js which is a client side JavaScript templating engine (which has equivalents in PHP/Java/Ruby/Python/Go and other languages and is based on Google CTemplates).

It will allow you to create HTML templates and populate them with the data you have in a variable such as the JSON variable an AJAX request might receive.

Friday, November 4, 2022
 
abhinav
 
4

Another solution is to set the values of the javascript variables to hidden form fields. Submit the form and read the form values on the server-side. After reading the values, create and send the email.

Wednesday, October 12, 2022
4

I guess your own comment has answered your question.

You only want the values to be passed on postback.

The only way browser and server can communicate is either via get request or post request over http protocol.

As you said you only want values to be passed on post back , then you can save the hashtable in hidden input and post or you can simulate post back using YUI as @Zoidberg has mentioned in his answer.

I do not see either using hidden input or using YUI or other JS library to simulate post back as less hacky or more hacky.

In both approach main idea is to communicate to server and those are two different ways to create a request which can be sent via browser to server.

and to answer your other question about best way to serialize and deserialize

I would recommend converting your javascript data to json string as per this example and pass that to server and using litjson to convert json data to .net objects.

Monday, November 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages