Jump to content

Php, Ajax, & Mysql help.


jamesxg1

Recommended Posts

Hiya peeps,

 

Im in need of some sort of snippet/tutorial/download of a code that can do the following,

 

basically i need to have say 5 text fields with links at the top Eg. select.php?id{ID HERE} and once one of the links are click it will populate the text fields with a mysql query from the {ID} selected,

 

any ideas anyone ?,

 

Many thanks,

 

James.

Link to comment
https://forums.phpfreaks.com/topic/168852-php-ajax-mysql-help/
Share on other sites

Do you have any code?  I'll gladly help you revise your current code, but I'm not huge on writing the whole thing from the ground up.

 

Hiya,

 

I haven't no, I was hoping for say a tutorial or some sort of downloadable snippet :),

 

And i dont blame you mate lol :),

 

Many many thanks,

 

James.

Link to comment
https://forums.phpfreaks.com/topic/168852-php-ajax-mysql-help/#findComment-890901
Share on other sites

here's some scripts that I use to do some work:

ajaxrequest.js

function MyAjaxRequest(file)
{
var MyHttpRequest = false;
var MyHttpLoading = '<p>Loading...</p>'; // or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />';
var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead';
var check_value = '';
if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product
{
	try
	{
		MyHttpRequest = new XMLHttpRequest();
	}
	catch(e)
	{
		MyHttpRequest = false;
	}
}
else if(window.ActiveXObject) // client use Internet Explorer
{
	try
	{
		MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			MyHttpRequest = false;
		}
	}
}
else
{
	MyHttpRequest = false;
}



if(MyHttpRequest) // browser supports httprequest
{
	var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching

	var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file
	if(file_array[1] == 'php') // no query string, just calling a php file
	{
		var query_string = '?rand=' + random;
	}
	else if(file_array[1] == 'htm' || file_array[1] == 'html') // calling a htm or html file
	{
		var query_string = '';
	}
	else // we have presumable a php file with a query string attached
	{
		var query_string = check_value + '&rand=' + random;
	}


	MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET


	// handle the httprequest
	MyHttpRequest.onreadystatechange = function ()
	{
		if(MyHttpRequest.readyState == 4) // done and responded
		{
			document.getElementById('quote').innerHTML = MyHttpRequest.responseText; // display result
		}
		else
		{
			document.getElementById('quote').innerHTML = "<img src='ajax-loader.gif'> Please Wait, Loading Results..."; // still working
		}
	}
	MyHttpRequest.send(null);
}
else
{
	document.getElementById('quote').innerHTML = ErrorMSG; // the browser was unable to create a httprequest
}
}
function url_encode(string)
{
var string;
var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=";
var hex = "0123456789ABCDEF";
var encoded_string = "";
for(var i = 0; i < string.length; i++)
{
	var character = string.charAt(i);
	if(character == " ")
	{
		encoded_string += "+";
	}
	else if(safechars.indexOf(character) != -1)
	{
		encoded_string += character;
	}
	else
	{
		var hexchar = character.charCodeAt(0);
		if(hexchar > 255)
		{
			encoded_string += "+";
		}
		else
		{
			encoded_string += "%";
			encoded_string += hex.charAt((hexchar >> 4) & 0xF);
			encoded_string += hex.charAt(hexchar & 0xF);
		}
	}
}
return encoded_string;
}

var quote = document.getElementById('quote');

 

ping.php

<html>
        <head>
                <script type="text/javascript" language="javascript" src="ajaxrequest.js"></script>
                <script type="text/javascript">
                function getURL(){
                        var field1 = document.getElementById('ip').value;
                        var field2 = document.getElementById('count').value;
                        var url = 'ping_backend.php?ip='+field1+"&count="+field2;
                        return url;
                }
                </script>
        </head>
        <body>
                <div id="wrapper">
                        IP/Domain: <input type="text"  name="ip" id="ip" /> Count: <input type="text" size="2" maxlength="2" name="count" id="count" />
                        <input type="submit" id="generate" onclick="MyAjaxRequest(getURL());" value="Submit">
                        <div id="quote"><p> </p></div>
                </div>
        </body>
</html>

ping_backend.php

<?php
$xml = simplexml_load_file("http://some-xml-url/xml.php?ip={$_GET['ip']}&count={$_GET['count']}");
//print_r($xml->PingResult->attributes());
$text_array = str_split((string)$xml->PingResult->attributes()->Text);
$success = 0;
foreach ($text_array as $key=>$val){
        $count = $key + 1;
        switch ($val){
                case "#":
                        echo "Ping $count: <strong>Success</strong><br />\n";
                        $success++;
                        break;
                case ".":
                        echo "Ping $count: <strong>Failure</strong><br />\n";
                        break;
        }
}
$percent = ($success / $count) * 100;
echo "<strong>$percent% Success Rate</strong><br />\n";
?>

 

xml.php (I have it hosted on a remote server that can ping internal IP addresses)

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// Number of times to Ping the host
$count = $_GET['count'];
// Host we are pinging
$url = $_GET['ip'];
// This is the Pear Net_Ping class call
require_once "Net/Ping.php";
$ping = Net_Ping::factory();
$ping->setArgs(array('count'=>1,'timeout'=>1));
// Set variables. I know I don't have to, but why not do things right?
$transmitted = 0;
$received = 0;
$loss = 0;
$text = "";

for ($i=0; $i< $count; $i++){
        //Send ping request
        $output = $ping->ping($url);
        // If the ping returns no errors, lets parse the results.
        if (isset($output->_transmitted)){
                // Raw data, if you want to see what it returned from the ping (                                                                                                                               full text)
                $raw = $output->_raw_data;
                // This is the transmitted count. if it's zero, that means it fa                                                                                                                               iled in transmission. We shouldn't get a zero, but this is to handle that instan                                                                                                                               ce
                $trans = (string)$output->_transmitted;
                // if it successfully transmitted, lets parse the result, and ad                                                                                                                               d it up.
                if ($trans == 1){
                        $transmitted++;
                        $received += (string)$output->_received;
                        if ((string)$output->_loss != 0){
                                $loss++;
                                $text .=".";
                        }

                        else{
                                $text .="#";
                        }
                }
                else{
                        $transmitted++;
                        $loss++;
                        $text .=".";
                }
        }
}
echo <<<EOF
<?xml version="1.0" encoding="utf-8" ?>
<PingResults Version='1.0'>
        <PingResult Host='$url' Text='$text' Trasmitted='$transmitted' Received=                                                                                                                               '$received' Loss='$loss' />
</PingResults>
EOF;
?>

I'll answer any questions you have regarding this code, or any other questions you have, if I know the answer.

Link to comment
https://forums.phpfreaks.com/topic/168852-php-ajax-mysql-help/#findComment-890905
Share on other sites

I learned AJAX about 2 weeks ago (yeah, I know.....), and I've gotten ok at it. I used this site as my starting point to learning the basics.

 

Lol well thats two weeks more knowledge than i have lmao,

 

I have a look see what i can do :),

 

Thanks dude much appreciated.

 

 

James.

Link to comment
https://forums.phpfreaks.com/topic/168852-php-ajax-mysql-help/#findComment-890910
Share on other sites

Hi

 

Javascript is essential!

 

firstly download an Ajax javascript library. One I have been using is zxml.js that came from an Ajax book I bought (Professional Ajax 2nd Edition, published by Wrox ). Do a Google and find a copy to download.

 

Seondly set up your php page to return data. For now just knock up a test script that returns a constant (or a couple of different constants). Say call it "AjaxTest.php" and have it as something like this:-

 

<?php
switch (intval($_REQUEST['PassedVar']))
{
case 1 :
$returnVar = "Returning One";
break;
case 2 :
$returnVar = "Returning two";
break;
case 3 :
$returnVar = "Returning three";
break;
default :
$returnVar = "Returning default";
break;
}
echo $returnVar;
?>

 

You then need your normal page:-

 

<html>
<head>
</head>
<body>
<a href='javascript:DoAjax()'>Click here to try Ajax</a>
<div id='UpdateArea'>
</div>
</body>
</html>

 

Now you need to modify that to call the script to return something from the server.

 

Will put this out of code tags and colour code it to try and talk your though it.

 

<html>

<head>

<script type="text/javascript" src="zxml.js"></script>

<script type="text/javascript" >

var oXHR = zXmlHttp.createRequest();

var QueryString = "AjaxTest.php?PassedVar="+Math.floor(Math.random()*11);

oXHR.open("get",QueryString, true);

oXHR.onreadystatechange = function ()

{

if (oXHR.readyState == 4)

{

if (oXHR.status == 200 || oXHR.status == 304)

{

document.getElementById('UpdateArea').innerHTML = oXHR.responseText;

}

}

}

oXHR.send(null);

</script>

</head>

<body>

<a href='javascript:DoAjax()'>Click here to try Ajax</a>

<div id='UpdateArea'>

</div>

</body>

</html>

 

Red bit - include in the javascript Ajax library

 

Set up a new instance of the object

 

Set up the query string that you will use to contact the php script. In this instance it is just a simple query string calling AjaxTest.php and passing a single parameter called PassedVar which is being given a value of a random number between 0 and 10.

 

Set up the call, stating that you will be doing it as a GET and giving the query string

 

Set up a function that will be executed when the Ajax call returns a value

 

Check a few statuses on return

 

Assign the returned text into the div called 'UpdateArea'

 

You can make things far more complex (by returning data as multiple fields in XML format for example), but hopefully this will give you a simple idea of what is going on.

 

Please excuse any typos.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/168852-php-ajax-mysql-help/#findComment-890968
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.