Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    140
    Thanks
    52
    Thanked 0 Times in 0 Posts

    php mysql + ajax

    Hello, I am new about Ajax, Please take a look at my code.

    I did the AJax successfully and the page doesnt load to another page, but when I look at my phpadmin, the input name and the other doesnt saved. I only see the ID not the input name,surname,sex
    php mysql + ajax-ajax1.jpg

    heres my code

    Code:
    <?php include('connector.php');?>
    <style>
    
    #samp {background: #666; color: #ffffff; padding: 20px;} 
    
    
    </style>
    
    
    
    
    
    <script>
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("samp").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","ajax.php?",true);
    xmlhttp.send();
    }
    </script>
    
    
    
    <div id="samp">
    <form action="ajax.php" method="get">
    <input name="name" type="text" />
    <input name="surname" type="text" />
    <input name="sex" type="text" />
    
    <input type="button" value="send" onclick="loadXMLDoc()" />
    
    </form>
    
    </div>


    Code:
    <?php include('connector.php');?>
    <?php 
    
    $reg = mysql_query("insert into registered values ('','$_GET[name]','$_GET[surname]','$_GET[sex]')");
    	
    	if ($reg){
    		echo "student successfully registered";
    	}
    		else {echo "failed to register";
    	}
    
    ?>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,710
    Thanks
    25
    Thanked 660 Times in 659 Posts
    This is a javascript question.
    First if you do
    Code:
    document.getElementById("samp").innerHTML=xmlhttp.responseText;
    The return will delete the form! Not what you want. Make a div to write te message to.
    Next: Your not sending the information to the PHP file. See my code below.
    Last
    Code:
    $_GET[name]
    in the PHP should be written
    Code:
    $_GET['name']
    and I'd set up variable and use them in the query.
    The HTML:
    Code:
    <?php include('connector.php');?>
    <style>
    
    #samp {background: #666; padding: 20px;}
    
    
    </style>
    <div id="samp">
    <form action="ajax.php" method="get">
    	Name<input name="name" id="name" type="text" /><br>
    	<input name="surname" id="surname" type="text" />
    	<input name="sex" id="sex" type="text" />
    
    	<input type="button" value="send" onclick="loadXMLDoc()" />
    </form>
    <div id="sample"></div>
    </div>
    
    
    <script>
    function loadXMLDoc(){
    	var xmlhttp;
    	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp=new XMLHttpRequest();
    	}else{// code for IE6, IE5
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function(){
    		if (xmlhttp.readyState==4 && xmlhttp.status==200){
    			document.getElementById("sample").innerHTML = xmlhttp.responseText;
    		}
    	}
    	name = document.getElementById('name').value;
    	surname = document.getElementById('surname').value;
    	sex = document.getElementById('sex').value;
    	xmlhttp.open("GET","ajax.php?name="+name+"&surname="+surname+"&sex="+sex,true);
    	xmlhttp.send('');
    }
    </script>
    And an example of the PHP file:
    PHP Code:
    <?php

    $name 
    $_GET["name"];
    $surname $_GET["surname"];
    $sex $_GET["sex"];

    echo 
    "NAME IS ".$name.'<br>';
    echo 
    "surname IS ".$surname.'<br>';
    echo 
    "sex IS ".$sex.'<br>';

    /*
    $reg = mysql_query("insert into registered values ('','$name','$surname','$sex')"); THIS SHOULD WORK FOR YOU
    echo $reg;
    /*    if ($reg){
            echo "student successfully registered";
        }
            else {echo "failed to register";
        }
    */
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    finoy_ako (04-30-2014)


  •  

    Posting Permissions

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