Results 1 to 9 of 9

Thread: [RESOLVED] JS seems to be clearing loaded PHP function (?)

  1. #1
    Join Date
    Apr 2009

    resolved [RESOLVED] JS seems to be clearing loaded PHP function (?)

    Hopefully viewers know a bit of PHP as well as JS to assist with this issue. The PHP function below sets up a series of links handling data by the JS function seen after:

    PHP Code:
    function Navibar()
            <div class="menubuttonsl"><a class="home_out" title="HOME" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'HomePage', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsl"><a class="email_out" title="MESSAGES" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'Messages', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsl"><a class="notify_out" title="NOTIFICATIONS" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'Notifications', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsl"><a class="mine_out" title="MY STUFF" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'MyStuff', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsl"><a class="comm_out" title="COMMUNITY" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'Community', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsl"><a class="live_out" title="LIVE ACTION" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'LiveAction', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsl"><a class="hot_out" title="WHATS HOT" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'WhatsHot', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsr"><a class="im_out" title="INSTANT MESSENGER" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'IMessenger', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
            <div class="menubuttonsr"><a class="upload_out" title="UPLOADER" onclick="ajaxrequestupload('include/profileview.php', '_upload', 'content2');"></a>
            <div id='username_upload' style="text-align:center;color:rgba(102,255,255, 0.0);font-size:1px">upload</div></div>
            <div class="menubuttonsr"><a class="rate_out" title="RATE PIX" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'RatePics', '',  '',  '',  '', '',  '',  '', 'content2');"></a></div>
    function ajaxrequest(php_file, purpose, var1, var2, var3, var4, var5, var6, var7, var8, where) {
      var request =  getXMLHTTP();		// call the function for the XMLHttpRequest instance
      // create pairs index=value with data that must be sent to server
      var  the_data = 'purpose='+purpose+'&var1='+var1+'&var2='+var2+'&var3='+var3+'&var4='+var4+'&var5='+var5+'&var6='+var6+'&var7='+var7+'&var8='+var8;
      request.open("POST", php_file, true);			// set the request
      // adds  a header to tell the PHP script to recognize the data as is sent via POST
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      request.send(the_data);		// calls the send() method with datas as parameter
      // Check request status
      // If the response is received completely, will be transferred to the HTML tag with tagID
      request.onreadystatechange = function() {
    	if (request.readyState == 4) {
    	  document.getElementById(where).innerHTML = request.responseText;
    PHP Code:
    (E_ALL); //here for obvious reasons
    session_start(); //apparently needed since somehow the session closes prior to coming here tho cannot find why
    require '_AllFunctions.php'//with reloading this already 'required' PHP file, none of the functions this page goes to exist...again dunno why since this file is 'required' and session_start() are on my index page
    global $db;
    if (isset(
    $_POST['purpose'])) {$purpose $_POST['purpose'];}
    if (isset(
    $_POST['var1'])) {$var1 $_POST['var1'];}
    if (isset(
    $_POST['var2'])) {$var2 $_POST['var2'];}
    if (isset(
    $_POST['var3'])) {$var3 $_POST['var3'];}
    if (isset(
    $_POST['var4'])) {$var4 $_POST['var4'];}
    if (isset(
    $_POST['var5'])) {$var5 $_POST['var5'];}
    if (isset(
    $_POST['var6'])) {$var6 $_POST['var6'];}
    if (isset(
    $_POST['var7'])) {$var7 $_POST['var7'];}
    if (isset(
    $_POST['var8'])) {$var8 $_POST['var8'];}

    if (
    PHP Code:
    function MyStuff()
        <div class='mainlinks'>
        <a class='inline' href="#inline_content_1">One</a> | <a class='inline' href="#inline_content_2">Two</a> | <a class='inline' href="#inline_content_3">Three</a> | <a class='inline' href="#inline_content_4">Four</a>
    Okay, that should be all the code that is relevant to this issue. Here's the issue:
    The PHP function MyStuff produces 4 links to anchor tags on the index.php. If I script my PHP to bypass the function Navibar and go directly to function MyStuff, the anchor tags work. But thru Navibar, I pass variables to the JS function ajaxrequest, which in turn sends JS variables to ajaxprocess.php for processing to PHP and then to the appropriate PHP function (in this case, MyStuff), the anchors cease to work. Links are show but nothing happens. If you are wondering why all this crap just to run a PHP function, short answer is because these links refresh DIVs without reloading the entire page.
    Thru extensive testing, I feel I have narrowed the issue down to something either in the JS shown or the ajaxprocess.php script shown. When I bypass both, everything works. But I cannot do the refreshing of the DIV without both.
    I am extremely new to JS. In fact the code is cobbled together bits and pieces of other developers scripts. It works for other links as I had hoped but using it to access the PHP function MyStuff fails.
    Also I'm concerned as to why when going from my PHP functions to ajaxrequest then to ajaxprocess.php I'm forced to restart an already running session_start (no I do not close it prior) as well as 'require' the _AllFunctions.php again even tho it is done upon arrival to my site.
    Any insight would save my hair going whiter (LOL)

  2. #2
    Well for starters this looks like using AJAX to avoid pageloads -- something that is an inaccessible mess and should be avoided at all costs. I would swing an axe at 90%+ of that code and just let pages load how pages are supposed to load so search engines, screen readers, and other non-scripting "user agents" will have more than a vague chance at actually seeing your content.

    The very concept of avoiding a pageload when it still does the same handshake is just goofy nonsense, and really not needed if you've coded using separation of presentation from content, minimalist markup, and leveraged caching models for your JS, used a monolithic stylesheet to pre-cache subpages appearance, and on the whole bother using HTML and CSS properly.

    This is a stunning example of failing to obey the unwritten rule of JavaScript: "If you can't make a fully functional page without JavaScript first, you likely have no business adding scripting to it.".

    ... and even if I were to use AJAX for something like that, I would NOT be using onclick in the markup, I'd have the anchors have proper href since some browsers won't even obey clicking on anchors that don't have href and you should have a scripting off fallback, I'd have actual text inside the anchors even if they are receiving style so I could use something like gilder-levin if those are getting images or some such (since title only is rubbish accessibility), etc, etc... Make the page work WITHOUT scripting FIRST... Have the pages share template data for their direct calls, but keeping their content called separately.

    ...then if you want the inaccessible unable to be hot-linked train wreck of recreating frameset behavior with AJAX, your script should hook onto those anchors and trap onclick itself instead of pissing all over the markup with the onclick attribute. Parse the requested URI in the anchor's href when the click event fires, then send it as getData or by some other means to your AJAX handling PHP to pull the same contents and plug it in, being sure to update the shown address while at it.

    I might take a stab later tonight at making a working example of that so you can at least see what I mean.

    Though really I would advise against wasting bandwidth and making the page less useful with AJAXtardery in the first place. AJAX can be really handy on things like posting to discussions, but using it to actually load your page's content is just asking for a steaming pile of /FAIL/ on the accessibility and functionality front, which is why it's rarely used outside of sites like FaceBook where the content is a steady flow of second by second updates and they could give a flying fig about search engines seeing it, indexing it, or showing the results. Using it on static content? Not a great idea as it's basically the same as replicating the frameset behavior, something that's been recognized as an inaccessible mess almost from the day it was introduced.

    You've kind-of got a visit to Doctor Hackenbush going on here. "Doctor, Doctor, it hurts when I do this!"

  3. #3
    Join Date
    Apr 2009
    Yeah helpful as always deathshadow. I do hope a moderator sees how you seem to be stalking my posts just to throw mud with no advice and bans you. I can bloody well design my site to have zero reload if I choose and is not even remotely close to the issue at hand.

  4. #4
    WAIT, you do know that JavaScript can't call PHP functions directly, right? The way you worded that is... off.

  5. #5
    Quote Originally Posted by killinspre View Post
    no advice
    NO ADVICE? That post was nothing but advice, advising you not to do that, and if you WERE to do that outlining how it should be done, WITH an offer of making a working example of how to approach it if you insist.

    Well, you can kiss that offer of a working example goodbye. Once again you just don't seem to want to be helped or listen to advice.

  6. #6
    Join Date
    Apr 2009
    Advice is not "do it my way cuz I don't like your way". I want my site the way I want my site. Dozens of testers so far love that there is no reload flicker. So keep your personal opinions to yourself when people are asking for script assistance.

  7. #7
    Join Date
    Apr 2009
    Quote Originally Posted by deathshadow View Post
    WAIT, you do know that JavaScript can't call PHP functions directly, right? The way you worded that is... off.
    Not really sure what you are referring to since 9 of the 10 links in the first PHP function have no issue sending variables from PHP to the JS and then to PHP. It is only the one links causing me issues. Focus on my code and not the grammar of my posting. Again I cannot think how to make things clearer for you to understand what is going on and how in regards to my site. If you do not understand the topic, scripts, or question just simply move on to another thread instead of nit-picking.

  8. #8
    Join Date
    Mar 2007
    Few observations... After playing around with the very limited code you have supplied, I can find several problems that exist.

     request.send(the_data);		// calls the send() method with datas as parameter
      // Check request status
      // If the response is received completely, will be transferred to the HTML tag with tagID
      request.onreadystatechange = function() {
    	if (request.readyState == 4) {
    	  document.getElementById(where).innerHTML = request.responseText;
    Your AJAX code, really should have the request.send(the_data) as the last propert in your code, this is more out of tidyness and ease of reading the code.

    Also, your HTML is lacking, your anchors have no href value or inner text that allows them to be used as links for example, when I put your code in to a web page, I had to add elements to it to make it work.
    <a class="home_out" href="javascript:;" title="HOME" onclick="ajaxrequest('include/ajaxprocess.php', 'refresh', 'HomePage', '',  '',  '',  '', '',  '',  '', 'content2');">Home</a></div>

    With the PHP side of things...
    PHP Code:
    if (isset($_POST['purpose'])) {$purpose $_POST['purpose'];} 
    if (isset(
    $_POST['var1'])) {$var1 $_POST['var1'];} 
    if (isset(
    $_POST['var2'])) {$var2 $_POST['var2'];}
    the issue you have here is that you have no kind of input control or sanitizing happening. This is very important, not because of injection attempts but your PHP script could be compromised in such a way that someone could break your PHP page to upload a PHP script that executes and gives unrestricted access to your server and services.

    Suggest that you use a whitelist and sanitize variable that you use instead of taking the raw data from the $_POST array.

    Anyway, I got it working based on your scripting with some changes
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  9. #9
    Join Date
    Apr 2009
    \\.\ thanks for your reply. I found similar issues when I got enough sleep and got it working properly. Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.20477 seconds
  • Memory Usage 2,974KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (4)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates