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 10: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 11: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



Recent Articles