Results 1 to 4 of 4

Thread: [RESOLVED] Javascript reloading page, or crashing, don't know why

  1. #1
    Join Date
    Jul 2011

    resolved [RESOLVED] Javascript reloading page, or crashing, don't know why

    (warning long post, sorry)

    I'm pretty new at PHP-Ajax but I've worked my way way around it so far.. until now....

    I'm trying to display content dynamically in a DIV

    My Script is a merge of two other scripts I found online one function (showUser) uses a GET method and the other (funcionPost) uses a POST method independently they work like a charm, but combined in the same script they throw some issues.

    PHP Code:
    <script type="text/javascript">
    alert("Comienzo Script");
    getXMLObject()  //XML OBJECT
    {  alert("Comienzo ~ getXMLObject()");
    xmlHttp false;

       try { 
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); alert("FLAG ~ EXPLORER 6-"); /*For Old Microsoft Browsers*/ }
       catch (
    e) {     try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  alert("FLAG ~ EXPLORER 6+");/* For Microsoft IE 6.0+*/ }
                       catch (
    e2) { xmlHttp false;   /*No Browser accepts the XMLHTTP Object then false */ }
       if (!
    xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); alert("FLAG ~ FIREFOX,CHROME,OPERA");/*For Mozilla, Opera Browsers*/}
    xmlHttp;  // Mandatory Statement returning the ajax object created
    alert("FIN ~ getXMLObject()");

    var xmlhttp = new getXMLObject();//xmlhttp holds the ajax object

    function handleServerResponse() {alert("Entra al server response");
                                      if (
    xmlhttp.readyState==&& xmlhttp.status==200)
    alert("Server response ~ if (xmlhttp.readyState==4 && xmlhttp.status==200)");
    document.getElementById("txtVP").innerHTML=xmlhttp.responseText//Update the HTML Form element PROBLEM COULD BE HERE
                                      else {
    alert("Error al llamado AJAX");}
    alert("Sale del server response");

    function funcionPost(opcion) {
    getdate = new Date();  //Used to prevent caching during ajax call
    if(opcion == 1)
    xmlhttp) { 
    rut document.getElementById("rut");
    nombre document.getElementById("nombre");
    apellido document.getElementById("apellido");
    telefono document.getElementById("telefono");
    email document.getElementById("email");
    xmlhttp.open("POST","vistapreliminarusuario.php",true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  handleServerResponse;
    alert("Entro a funcionPost con cadena=  ""rut=" rut.value "&nombre=" nombre.value "&apellido=" apellido.value "&telefono=" telefono.value "&email=" email.value +" FECHA "getdate.value);
    xmlhttp.send("rut=" rut.value "&nombre=" nombre.value "&apellido=" apellido.value "&telefono=" telefono.value "&email=" email.value); //Posting txtname to PHP File
    opcion == 2)
    xmlhttp) { 
    txtname1 document.getElementById("txtname1");
    txtname12 document.getElementById("txtname12");

    xmlhttp.open("POST","testing2.php",true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  handleServerResponse;

    xmlhttp.send("txtname1=" txtname1.value "&txtname12=" txtname12.value); //Posting txtname to PHP File
    alert("Sale de funcionPost");

    function showUser(str)
    alert("Entro a showUser con str="+str);
                                if (
    xmlhttp.readyState==&& xmlhttp.status==200)
    document.getElementById("txtSalida").innerHTML=xmlhttp.responseText//OR PROBLEM MIGHT BE HERE

    alert("sale de showUser");


    alert("Fin Script");
    There are lot's of alerts to keep track on things, but still can't determing whats wrong, perhaps due to my lack of experience with javascript...

    I'll run down the behavior of the script when using it...
    some alerts are in spanish since i'm Chilean.

    *loads the page creates object for FIREFOX*

    Comienzo Script // script start
    Fin Script // script end

    *click on drop down menu and select option 1 that uses showUser function*

    Entro a showUser con str="+str // Enters to showUser function where str is the value selected from the drop down menu, in this case 1.
    Sale de showUser //exits showUser function, displays form in DIV with id "txtSalida"

    *I fill out the form and press the submit button, the idea is to show a preview of the input data for confirmation, the preview DIV is called "txtVP"*

    *once the submit button is pressed( onclick="funcionPost(1);" )*

    Entro a funcionPost con cadena= "+ "rut=" + rut.value + "&nombre=" + nombre.value + "&apellido=" + apellido.value + "&telefono=" + telefono.value + "&email=" + email.value +" FECHA "+ getdate.value // this alert show that it entered the funcionPost function and displays the data that was in the form.


    *It will enter the handleServerResponse function and loop 3 times between*

    Entra al server response // Enters server response
    Error al llamado AJAX // Error in ajax call
    Sale del server response // Exits server response

    *In the fourth iteration it will display the information correctly in the txtVP DIV, my gues is that it takes 4 times because of the readyState, but that's just my guess*

    Server response ~ if (xmlhttp.readyState==4 && xmlhttp.status==200)

    *So far I would be happy with the results, since it actually displays stuff in the way I like it... regardles of the 3 previous error iterations... but then... it iterates one more time..dunno why...*

    Entra al server response // Enters server response
    Server response ~ if (xmlhttp.readyState==4 && xmlhttp.status==200)
    Sale del server response // Exits server response


    Comienzo Script // script start
    Fin Script // script end

    *which reloads the whole thing and i'm back at square 1*

    Without the alerts to track every move... everything happens in fractions of second.. due to tracking I was able to determine that it actually displays the info correctly, BUT then reloads the whole thing...

    I know it's a lot to ask..... but i'm feeling hopeless here and perhaps you can see the error more evidently than me....

    I would appreciate greatly your help...
    or even if you just took the time to read all this... I'm in gratitude...

    thank you in advance and looking forward to your questions, comments or replies

  2. #2
    Join Date
    Jul 2011
    PLAN B
    If my original post was TOO LONG...which I know it is... perhaps you can help me with some doubts I would appreciate that too!

    PHP Code:
    what's application/x-www-form-urlencoded for?

    I understand that libraries like jquery do the ajax handling, POST and GET methods, do I need to install something on the server-side? should I go with this option? I feel i-m pretty close to get the results I want.. I just need to figure out why my page reloads at the end...

  3. #3
    Join Date
    Jul 2011
    the form is submited with this button button.. does type="submit" forces page reload??

    PHP Code:
    <div id="buttonDiv">
    input type="submit" id="submitButton" name="submitButton" value="Ingresar" onclick="funcionPost(1);"/>

  4. #4
    Join Date
    Jul 2011

    Changed the button type on the form to "type=button"

    now the page won't reload..


    It still alerts me 3 times of AJAX call error before displaying the correct response text.. perhaps I should just disable that else sentence.. but i-m afraid there's a deper meaning to that error... dont want to leave it just like that!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.11019 seconds
  • Memory Usage 2,936KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (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_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)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 (73):
  • 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_postinfo_query
  • fetch_postinfo
  • 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
  • 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