Converting php script into Ajax {how it will possible }

Discussion in 'PHP' started by ironmankho, Jul 27, 2008.

  1. #1
    First of all thanks Mr.hackyia to complete this php script from my 1st post
    http://forums.digitalpoint.com/showthread.php?t=949982

    also thanks for mr alhelalat who effort for me :)

    [​IMG]

    mention below script is same that i want (thanks Mr.hackyia )

    <?php
    $admin = "xxxxxxx@xxxxxx.xxxx"; // Administrator Email
    
    // if (!defined(MAILER)) die("Hacking attempt");
    if (!empty($_POST["error"])) {
        if (count($_POST["error"]) == 1)$message = "Hello,\n\nAn error \"" . $_POST["error"][0] . "\" was reported by a user of your website at page " . $_SERVER["REQUEST_URI"] . " on " . date("g:i A (P) d F Y");
        if (count($_POST["error"]) > 1) {
            $message = "Hello,\n\n The following errors were reported by a user of your website at page " . $_SERVER["REQUEST_URI"] . " on " . date("g:i A (P) d F Y") . " -";
            foreach($_POST["error"] as $error) {
                $message .= "\n" . $error;
            }
        }
       
        if (mail($admin, "Error in website", $message)) {
            echo "<p style=\"font-weight: bold\">Thank you, the email has successfully been sent to the administrator.</p>"; // Email sent message
        }
        else {
            echo "<p style=\"font-weight: bold\">Sorry, email could not be sent due to an error.</p>"; // Email not sent message
        }
        die();
    }
    
    ?>
    
    <table>
    <form method="post" action="reporterror.php">
    <tr><th>Please tell us what is wrong with this page-</th></tr>
    <tr><td><input type="checkbox" name="error[]" value="Broken Download Link">Broken Download Link&nbsp;&nbsp;<input type="checkbox" name="error[]" value="Abuse Issues">Abuse Issues&nbsp;&nbsp;<input type="checkbox" name="error[]" value="Other Error">Other Error</td></tr>
    <tr><td><input type="submit" value="Report Error"></td></tr>
    </form>
    </table>
    PHP:

    now i want only one funtion .how it will possible convert into ajax .means when user press "Report error" it will done on same page .i hope you understand
     
    ironmankho, Jul 27, 2008 IP
  2. Boxerman

    Boxerman Peon

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I dont understand
     
    Boxerman, Jul 27, 2008 IP
  3. ironmankho

    ironmankho Active Member

    Messages:
    393
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    simple i want when user press "submit button" then it will done on same page not ..... going to further

    simply you can understand

    when we press "post Quick reply " in DP forum it done on same page if you press "Go Advanced" it will going to next page
     
    ironmankho, Jul 27, 2008 IP
  4. mbreezy

    mbreezy Active Member

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #4
    I have a little system I set up for all my ajax operations....

    Create a file called ajax.js
    
    var xmlHttp
    var box
    
    function ajaxBuddy(url, id, loader)
    { 
    	box = id
    	xmlHttp=GetXmlHttpObject()
    	if (xmlHttp==null)
    	{
    	  	alert ("Browser does not support HTTP Request")
    	  	return
    	}
    	document.getElementById(box)
    	.innerHTML=loader;
    	xmlHttp.onreadystatechange=stateChanged
    	xmlHttp.open("GET",url,true)
    	xmlHttp.send(null)
    }
    
    
    
    function stateChanged() 
    { 
    	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    	{ 
      		document.getElementById(box)
      		.innerHTML=xmlHttp.responseText 
      	} 
    }
    function GetXmlHttpObject()
    {
    	var xmlHttp=null;
    	try
    	{
    		// Firefox, Opera 8.0+, Safari
    		xmlHttp=new XMLHttpRequest();
    	}
    	catch (e)
    	{
    		// Internet Explorer
    		try
    		{
      			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      		}
    		catch (e)
      		{
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    	}
    	return xmlHttp;
    }
    
    Code (markup):
    Next you need to take your PHP code and put it into another file (in the code below I called it ajaxhandler.php) and remove it from the current file.

    
    <script src="ajax.js" type="text/javascript"></script>
    <div id="errorarea">
    <table>
    <tr><th>Please tell us what is wrong with this page-</th></tr>
    <tr><td><input type="radio" id="errormsg" name="errormsg" value="Broken Download Link">Broken Download Link&nbsp;&nbsp;<input type="radio" id="errormsg" name="errormsg" value="Abuse Issues">Abuse Issues&nbsp;&nbsp;<input type="radio" id="errormsg" name="errormsg" value="Other Error">Other Error</td></tr>
    <tr><td><input type="submit" onClick="ajaxBuddy('ajaxhandler.php?error='+document.getElementById('errormsg').value, 'errorarea', 'sending...');" value="Report Error"></td></tr>
    </table>
    </div>
    
    Code (markup):
    Here's what I've done... Put a div around your table and gave it an id of 'errorarea'. I changed your checkboxes to radios, you can do whatever. Then the submit button simply runs ajaxBuddy() on the click. There is no form to submit.
    If you look at the ajaxBuddy() function, the first parameter is the URL of the PHP file to execute (the one you just put your code into) with parameters you want to send. The second parameter is the div box you want the results to be displayed into. As of right now it will replace your table with the output of the php file, but you can relocate that to whereever you want. The last parameter is what you want the div to show while it's loading. You can put "loading..." or "sending..." or you can even put in one of those ajax loading gifs. Yes, HTML can go in there.

    I think that's it. Not sure if my code is error free because I simply coded it into the quick reply box here. That should work for ya though. Lemme know!

    Edit: be advised that my code is always dirty, it works but there's always more efficient ways. This is something for you to play with. Enjoy
     
    mbreezy, Jul 27, 2008 IP