Jump to content

Javascript parameter passing in PHP script


tokool420

Recommended Posts

Hello,

 

I have below javascript, which I need to put in a new php page. However I need the vistiors to pass in the username to the script either by clicking the link  for that user or by entering a value on the website.

 

I am not sure how can I do this in php script and modify below java script to make it work.

 

Any help will be greatly appreciated.

 

<pre class="example">

    $(function(){

      $(".tweet").tweet({

        join_text: "auto",

        username: "NEED_TO_BE_PARAMETER",

        avatar_size: 68,

        count: 25,

        auto_join_text_default: "said,",

        auto_join_text_ed: "I",

        auto_join_text_ing: "I wase",

        auto_join_text_reply: "I replied",

        auto_join_text_url: "I was checking out",

        loading_text: "loading..."

      });

    });

  </pre>

 

  <div class='query'></div>

  <script type="text/javascript">

    $(function(){ $(".example").each(function(i, e){ eval($(e).text()); }); });

  </script>

 

 

The simplest solution that come to mind is printing PHP directly to the Javascript code, but that wouldn't be very clever. I'm talking about something like below, where POST data is printed directly in the Javascript code.

 

$(function(){
      $(".tweet").tweet({
        join_text: "auto",
        username: "<?php echo $_POST['username']; ?>",
        avatar_size: 68,
        count: 25,
        auto_join_text_default: "said,",
        auto_join_text_ed: "I",
        auto_join_text_ing: "I wase",
        auto_join_text_reply: "I replied",
        auto_join_text_url: "I was checking out",
        loading_text: "loading..."
      });
    });

 

The best method would be using just Javascript and no PHP. You can make an input and a button, so when the user submits it, you catch that data with Javascript and pass it to the rest of the code you have. Something like:

 

//js code
$('button').click(function(){
     var username = $('#username').val(); //this is the username
});

//html code
<input type="text" name="username" id="username" />
<button type="button">Submit</button>

 

You'll need to provide alternate form submitting for non-javascript users, but as your functionality is based on Javascript, I guess you don't care.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.