www.webdeveloper.com
Results 1 to 14 of 14

Thread: Ajax request is not working

  1. #1
    Join Date
    Aug 2012
    Posts
    9

    Ajax request is not working

    Hi all, I have a problem, trying make ajax request, but it doesn't work. I need make request without refreshing page. I have no experience in js, may be somebody can help me. Below is my variant of code:
    HTML Code:
    <form id=payment method="post" name="forma1" action="">
            
                                      <label for=name>Vvedite sait:</label>
                                      <input id=name type=text placeholder="Here" required autofocus name="site">
     
             
                <div align="left" style="padding-right:55px;">
                    <input type="submit" value="start" name="searchbutton" id="sb">
                </div>
              
          </form>
    Code:
    (function($){
    			var form;
    			$('#payment').submit(function(e){
    				form = this;
    				e.preventDefault();
    				var response = $.ajax({
    					type: 'POST',
    					url: this.action,
    					data: $(this).serialize(),
    					dataType: 'json',
    					
    					success: successResult()
    					{
    						
    						alert("ok");
    						
    					}
    				});
    				 return false;
    			});
    		})(this.jQuery);

  2. #2
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66
    <label for="name">Vvedite sait:</label>
    <input id="name" type="text" name="url"></input>


    $('#ib').click(function(){
    var url = $('#name').attr('name');
    var name = $('#name').html();
    url = url + "?name="+name
    $.ajax({
    url: url,
    dataType: 'json',
    complete: function(jqXHR, responseText){
    x = (jqXHR.responseText);
    alert("x");
    }
    });
    });

  3. #3
    Join Date
    Aug 2012
    Posts
    9
    Thanks for your help, but it doesn't work ((

  4. #4
    Join Date
    Aug 2012
    Posts
    9
    page reloads, but I need without reloading

  5. #5
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66
    <form id="payment" name="forma1">

    you don't need action because the action is done by the script

    <input type="button"> in place of submit

    if(isset($_GET[''])){
    {

    echo "Field cannot be empty";
    return;

    else{
    //some action
    }
    Last edited by Gudlife; 08-29-2012 at 09:52 AM.

  6. #6
    Join Date
    Aug 2012
    Posts
    9
    M yes, now without reloading. But when I make <input type="button">, then my php script is not working. What the reason?
    PHP Code:
    if ($_POST['searchbutton'] == true)
    {

    echo 
    "Field cannot be empty";
    return;

    else{
    //some action


  7. #7
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66
    it needs an id, in this case id="ib", because the event listener is listening that one

    => $('#ib').click(function(){...

  8. #8
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66

    also

    you need to change your php to:

    if(isset($_GET['name'])){

    }
    else{

    }

    and not return your result but print it...

    print $result;

  9. #9
    Join Date
    Aug 2012
    Posts
    9
    Ok, now i use ID instead NAME, like this:
    PHP Code:
    if ($_POST['sb'] == true
    But I have no action when click on button:
    Mmm i don't know, may be you can see where is error in my code:
    Full code:
    PHP Code:
    if ($_POST['sb'] == true)
    {
    //do action
    $homepage file_get_contents("http://".$_POST['site']);
    }
    else
    {
    //another case

    HTML Code:
      <form id=payment method="post" name="forma1">
    		
    								  <label for=name>Enter name:</label>
    								  <input id=name type=text placeholder="Write here..." required autofocus name="site">
    		
    		 
    		  <fieldset>
    			<div align="right" style="padding-right:15px;">
    				<input type="button" value="start" name="searchbutton" id="sb">
    			</div>
    		  </fieldset>
    	  </form>
    Code:
    $('#sb').click(function(){
    var url = $('#name').attr('name');
    var name = $('#name').html();
    url = url + "?name="+name
    $.ajax({
    url: url,
    dataType: 'json',
    complete: function(jqXHR, responseText){
    x = (jqXHR.responseText);
    alert("x");
    }
    });
    }); 
            });

  10. #10
    Join Date
    Aug 2012
    Posts
    9
    Not working (( The same situation, button without action

  11. #11
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66

    ^^

    $_POST will catch nothing because your are sending your data with a $_GET method.

    You can take method="post" out from your code, because it is not used anymore.

    personnaly, I catch the data this way:

    if(isset($_GET['name'])){ ... some action } else { ...other action}

    don't forget your are working ajax, and not actually sending the form, just the data.

  12. #12
    Join Date
    Aug 2012
    Posts
    9
    Please, if you have a time, could you see my full code again and test in your pc? I'm trying, but without results (

  13. #13
    Join Date
    Aug 2012
    Posts
    9
    PHP Code:
     <?php

    if(isset($_GET['searchbutton'])){


    $homepage file_get_contents("http://".$_GET['site']);
    preg_match('%<meta.*name="keywords".*content="(.*)"\s+/>%U'$homepage$regs);



    if(
    count($regs))
    {
        
    $myString implode(''$regs );  
         
    print_r($myString);
    }
    }


    ?>
    HTML Code:
    		  <form id=payment method="get" name="forma1">
    			  <label for=name>ENTER www.bbc.com:</label>
    			  <input id="name" type=text placeholder="Write here..." name="site">
    			  <input type="button" value="START" name="searchbutton" id="sb">
    		  </form>
    	
    	  
    		
    
    		<script type="text/javascript">
    		$('#sb').click(function(){
    		var url = $('#name').attr('name');
    		var name = $('#name').html();
    		url = url + "?name="+name
    		$.ajax({
    		url: url,
    		dataType: 'json',
    		complete: function(jqXHR, responseText){
    		x = (jqXHR.responseText);
    		alert("OK, Ajax is working!");
    		}
    		});
    		}); 
    		</script>
    Last edited by webber2012; 08-29-2012 at 10:54 AM.

  14. #14
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66
    // test.php

    <?php
    session_start();
    $array = array();
    $array['key'] = "value";
    $array['hello'] = "world";
    $_SESSION['array'] = $array;
    if(isset($_GET['url'])){
    foreach($_SESSION['array'] as $key=>$value){
    if($key == $_GET['url']){
    print($value);
    }
    }
    }
    else{
    print('an error has occured during this process: test.php ajax call');
    }
    ?>

    //index.php

    <script type="text/javascript" src="jquery-1.7.1.min.js"></script>

    <table>
    <tr>
    <td>enter the to search url: <input id="url" type="text"></td><td><input type="button" value="START SEARCH" id="sb"></td>
    </tr>
    </table>

    <script type="text/javascript">
    $('#sb').click(function(){
    var tosearch_url = $('#url').val();
    var url = "test.php?url="+tosearch_url;
    alert(url);
    $.ajax({
    url: url,
    dataType: 'json',
    complete: function(jqXHR, responseText){
    x = (jqXHR.responseText);
    alert("OK, Ajax is working! The returned url is: "+x);
    }
    });
    });
    </script>

    // test this by running index.php with input "hello"

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.57210 seconds
  • Memory Usage 3,001KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (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
  • (2)bbcode_code
  • (3)bbcode_html
  • (4)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (14)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (14)postbit
  • (14)postbit_onlinestatus
  • (14)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