www.webdeveloper.com
Results 1 to 9 of 9

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

  1. #1
    Join Date
    Apr 2009
    Posts
    35

    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:

    localhost/include/_AllFunctions.php
    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>
            <?php
        
    }
    localhost/js/myjs.js
    Code:
    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;
    	}
      }
    }
    localhost/include/ajaxprocess.php
    PHP Code:
    <?php
    error_reporting
    (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 (
    $purpose=='refresh')
        {
            
    $var1();exit;
        }
    ?>
    localhost/include/_AllFunctions.php
    PHP Code:
    function MyStuff()
        {
        ?>
        </div>
        <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>
        <?php
        
    }
    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
    Join Date
    May 2014
    Posts
    897
    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!"
    Java is to JavaScript as Ham is to Hamburger.

  3. #3
    Join Date
    Apr 2009
    Posts
    35
    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
    Join Date
    May 2014
    Posts
    897
    WAIT, you do know that JavaScript can't call PHP functions directly, right? The way you worded that is... off.
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    May 2014
    Posts
    897
    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.
    Java is to JavaScript as Ham is to Hamburger.

  6. #6
    Join Date
    Apr 2009
    Posts
    35
    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
    Posts
    35
    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
    Location
    localhost
    Posts
    2,346
    Few observations... After playing around with the very limited code you have supplied, I can find several problems that exist.

    Code:
     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.
    Code:
    <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
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  9. #9
    Join Date
    Apr 2009
    Posts
    35
    \\.\ 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



Recent Articles