Asked  7 Months ago    Answers:  5   Viewed   31 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";

to a PHP variable


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?



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.

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.


Wednesday, March 31, 2021
answered 7 Months ago

ok,from what i understand, you need a javascript function binded on the onclick event of the generated links that sends you back to a different page the link content. if that's what you need, i recommend using a library such as jquery (it's more easy ti use) and paste this code into the page:
{data : $(this).html()}); });

hope this helps :|

Saturday, May 29, 2021
answered 5 Months ago

Secure for 08/15 website: yes
Secure for online banking: no

The method you use is equivalent to an unencrypted everyday login <form>. Albeit you should really not rely on a "user_id" cookie. Rather save the verified user_id in the session store only.

Also you might try to simply return the session cookie on the JSON result for the AJAX call. It usually sticks to all further HTTP requests, so you don't need (3) to set the cookie via Javascript additionally.

Saturday, May 29, 2021
answered 5 Months ago

You are not redirected because PHP redirects do not affect the browser when run as an AJAX call. You'll need to specify a JavaScript redirect in the success function:

    type: 'post',
    url: 'update.php',
    data: {
        source1: "some text",
    success: function( data ) {
        document.location.href = 'update.php'

However, I don't believe that this is what you want. You seem to want to add new form data on submitting the form, in which it might be better to prevent the default behaviour, add any additional data with hidden input fields, and then re-submit the form:

  $(function ()

    $('form').submit(function (e)

      if ($(this).is(':not([data-submit="true"])'))

        $('form').append('<input type="hidden" name="foo" value="bar">')

        $('form').data('submit', 'true').submit()


        return false




Saturday, May 29, 2021
answered 5 Months ago

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.

Sunday, August 29, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :