www.webdeveloper.com
Results 1 to 2 of 2

Thread: php + ajax without refresh

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    57

    php + ajax without refresh

    Hi, In index.php I have code that allow me to change the text from "activate" to "Deactivate" or "Deactivate" to "activate' by clicking the link using ajax and then refresh in same page... and it will update in mysql database...please see the code ...

    HTML Code:
    	$.ajax({
    	type: "GET",
    	url: "action.php",
    	data: { status: "<?php echo $_GET['pid'];?>" }
    	})

    And in action.php page is ....


    PHP Code:
    <?php
    include('db.php');
    if(isset(
    $_GET['status']))
    {
    $status1=$_GET['status'];
    $select=mysql_query("select * from product where product_id='$status1'");
    while(
    $row=mysql_fetch_object($select))
    {
    $status_var=$row->status;
    if(
    $status_var=='0')
    {
    $status_state=1;
    }
    else
    {
    $status_state=0;
    }
    $update=mysql_query("update product set status='$status_state' where product_id='$status1' ");
    if(
    $update)
    {
    header("Location: index.php");
    }
    else
    {
    echo 
    mysql_error();
    }
    }
    ?>
    <?php

    ?>

    I was wondering if this possible to be done WITHOUT refresh the page after click the link to change text in index.php page!

    I'm not really good at the ajax!

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    you can have the action.php file output a "response" that the ajax will get and process.
    example:
    PHP Code:
    <?php
    if(array_key_exists('status'$_GET))
    {
        
    $data = (int) $_GET['status'];
        
    // store value in database...

        // test query success...
        
    if($query === false)
        {
            
    $response '{"error": "unable to store value."}';
        }
        else
        {
            
    $response '{"result": ' .$data'}'// JSON format
        
    }
    }
    else
    {
        
    $response '{"error": "invalid request"}';
    }
    // finally, send a response back to the client-side...
    header('Content-Type: application/json; charset=utf-8');
    exit(
    $response);
    ?>
    as for the javascript... i'm old school, i don't use frameworks. so i have no idea how to process the response in jQuery.
    without a framework it would look something like this
    Code:
    function toggle(data){
    	// process error
    	// or toggle the button
    }
    
    var http = new XMLHttpRequest();
    http.onreadystatechange = function(obj){
    	if(obj.status === 200){
    		if(obj.responseType == 'json' && obj.response)
    			toggle(obj.response);
    		else
    			toggle(JSON.parse(obj.responseText));
    	}
    };
    http.open('GET', 'action.php?status=' + encodeURIComponent(<?php echo '\'' .$_GET['pid']. '\''; ?>), true);
    http.setRequestHeader('Accept', 'application/json; charset=utf-8');
    http.send();
    Last edited by ShrineDesigns; 04-14-2014 at 06:34 AM.

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



Recent Articles