Jump to content

Xmlhttprequest() Alike Dynamic Page Should Always Combined With Asp?


sunicani
 Share

Recommended Posts

look, when I studied Ajax combined with XMLHttpRequest(), I got some extral file called 'time.asp',

<html><body><script type="text/javascript">function ajaxFunction(){var xmlhttp;if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }else { alert("Your browser does not support XMLHTTP!"); }xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4) { document.myForm.time.value=xmlhttp.responseText; }}xmlhttp.open("GET","time.asp",true);xmlhttp.send(null);}</script><form name="myForm">Name: <input type="text" name="username" onkeyup="ajaxFunction();" />Time: <input type="text" name="time" /></form></body></html>
my question is so much simple, that is, a XMLHttpRequest() for dynamic page should be often combined with ASP scripting??oh, who any experts can guide me in this question?thanks, mate.
Link to comment
Share on other sites

It can be combined with ASP, or with PHP. Or you can simply use it to obtain the contents of a static file. There's no better practise, it all depends on what your goal is when using AJAX.

Link to comment
Share on other sites

It can be combined with ASP, or with PHP. Or you can simply use it to obtain the contents of a static file. There's no better practise, it all depends on what your goal is when using AJAX.
well, if I want to obtain contents from server for shopping sites, which way is much better, I meant combined with PHP or ASP?I saw many websites use ASP?by the way, what will XML be used while getting contents from server?give me a hand, mate, thanks a lot
Link to comment
Share on other sites

There is no better. A lot of sites use ASP because their programmers are already familiar with one of ASP's languages (ASP is not a language per se). One of the most common development languages is Visual Basic, and that's also an ASP language. So a lot of programmers are able to move directly from application development to ASP. PHP has very little use outside of web development, so many programmers learn PHP from scratch. If they have experience with C, C++, or JavaScript, it's easier for them.PHP and ASP are both fully capable of doing all the tasks that servers need to do. They both have database API's, and they are both pretty fast.If you are already a master of one of the ASP languages, it might make sense to stick with that. If you already know a C-type language, or if you are also learning JavaScript, then PHP might make more sense.As for XML, that's up to you. The "XML" in XMLHttpRequest is misleading. You do not have to send or receive data in XML form. You can. But you can transmit data as any string of characters. If your database is already in XML format, then this ability might be useful to you. The HTML DOM provides convenient methods for accessing data from an XML object. On the other hand, XML data can be VERY long, which means transmission can be long. So the amount of data to be transmitted may be a guiding factor.

Link to comment
Share on other sites

Your example asks for the time and displays it. You have two problems. The first is your http source must have the time in a convenient form (or your shopping cart entries in a digestible format). The second problem is browsers tend to stop you from sending Ajax calls to sites other than the one originally sending the page , so you typically you do need server-side processing. What do you normally use?

Link to comment
Share on other sites

Your example asks for the time and displays it. You have two problems. The first is your http source must have the time in a convenient form (or your shopping cart entries in a digestible format). The second problem is browsers tend to stop you from sending Ajax calls to sites other than the one originally sending the page , so you typically you do need server-side processing. What do you normally use?
yeah, ah... to be honest, I am just starting server-side scripting for product information stored in MySQL database and, I do not know it is better to choose which scripting language, AJAX with PHP, or AJAX with ASP, ? maybe just pure PHP +MySQL?my purpose is simply, that is, choose a kind of easy and, relatively compatible, fast scripting language to start with.in addition, product information would be shown like below,
Product Name: xxxDescription: yyyReviews: xxxMerchant Listmerchant A, pricemerchant B, pricemerchant C, price...
Link to comment
Share on other sites

If you are starting from scratch, you may want to consider Ruby which is more modern then PHP or C# but with equally rich support. Another option is a JavaScript server-side implementation eg Aptana Jaxer, so you only have to learn one new language.

Link to comment
Share on other sites

Ruby is 14 years old now I think (1995?), I haven't used it much but I know it doesn't have a community as large as that for PHP or ASP. I've heard a lot of bad things concerning ruby with regard to stability and performance, I'm not sure if I would recommend it for a beginner. From what I've heard it's very easy to do anything that ruby was specifically designed for, but very hard to make it do anything else.

Link to comment
Share on other sites

I'm not sure if I would recommend Ruby for a beginner.
What about JavaScript (on the server)? You have to admit there is something perverse about using two completely different languages in an application.
Link to comment
Share on other sites

What about JavaScript (on the server)? You have to admit there is something perverse about using two completely different languages in an application.
Not really - every language is optimised for a certain function - JS with its powerful DOM manipulation functions, for example, and PHP with its great database integration and HTTP management. Languages are designed with a purpose in mind - why should one try to push them into multiple roles, just because "I'm already using it"?
Link to comment
Share on other sites

What about JavaScript (on the server)?
I haven't used Javascript on the server, I wouldn't really be able to say much about it. It might work well, but I wouldn't want to see a bunch of hacks to try to get Javascript to do something that it wasn't meant to do. But like I said, I haven't seen much about it. One thing I would be concerned about is trying to find a host that supports it. PHP is the single most popular Apache module in the world.
You have to admit there is something perverse about using two completely different languages in an application.
Not necessarily, one language I use for server tasks, and one language I use for the interface. There's a clear separation between them, they don't really get in the way of each other and each is suited exactly for what it's being used for. Since PHP can output to JSON format, all I really use PHP for is to get whatever information from the database is needed, and send it out to Javascript, or to update the database (plus sending email or whatever else needs to be done). I just don't really see an entire web application as one single piece, I see a real separation between the server and the interface. PHP and Javascript work pretty well together.Here's one example, this is my password reset procedure for one of my applications. This include file sets a password reset token and sends the user an email with a link to click to reset their password, this file gets executed via ajax when the user requests to have their password reset:
<?php$email = form_var('email');$username = form_var('username');if ($email != ''){  if (!validate_email($email))  {	$response['errors']['email'] = 'The email address was not a valid format.';	return;  }  $db->sql('SELECT id, username, email, fname, lname, active FROM users WHERE email=%s');  $db->add_param($email);  $user = $db->select();  if (!$user)  {	$response['errors']['email'] = 'The email address was not found.';	return;  }  $user = $user[0];}elseif ($username != ''){  $db->sql('SELECT id, username, email, fname, lname, active FROM users WHERE username=%s');  $db->add_param($username);  $user = $db->select();  if (!$user)  {	$response['errors']['username'] = 'The username was not found.';	return;  }    if (!validate_email($user[0]['email']))  {	$response['errors']['reason'] = 'That username does not have a valid email address associated with it.  An administrator must reset your password.  Once you regain access, enter a valid email address for your account.';	return;  }  $user = $user[0];}else{  $response['errors']['reason'] = 'The email address and username were blank.';  return;}if (isset($user) && $user != false){  if (!$user['active'])  {	$response['errors']['reason'] = 'That account is not active.  An administrator must activate the account before the password can be reset.';	return;  }  // generate token  do  {	$token = substr(sha1(time() . $_SERVER['REMOTE_ADDR']. mt_rand()), 0, 20);	$db->sql('SELECT COUNT(*) AS num FROM users WHERE pw_reset_token=%s');	$db->add_param($token);	$check = $db->select();  } while ($check[0]['num'] > 0);  // update DB  $db->update('users', array('pw_reset_token' => $token, 'pw_reset_time' => time(), 'pw_reset_ip' => $_SERVER['REMOTE_ADDR']), 'id=' . $user['id']);  // send email  $to = $user['email'];  $from = $opts->get_opt('system_email');  $subject = $config['lms_title'] . ' - Password Reset';  $body = <<< EOT{$user['fname']} {$user['lname']},This is a message from {$config['lms_title']}.  A request has been received to reset the password on your account.  If you made this request, visit this link to reset your password:{$config['http_root']}/password_reset.php?t={$token}Your username: {$user['username']}This link will automatically expire after 24 hours.If you did not request to have your password reset, you may ignore this email.EOT;  $sent = mail($to, $subject, $body, 'From: ' . $from);  // send response  if ($sent)	$response['info'] = 'An email was sent to ' . $user['email'] . ' with further instructions.';  else	$response['info'] = 'We attempted to send an email with instructions, but it was not accepted for delivery.';  $response['success'] = true;}?>

So all of that is pure PHP, there's no HTML or anything else in there. When they click the link, this is the page they load:

<?phprequire_once 'include/global.init.php';define('LMS_DEBUG', false);$token = form_var('t');?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>  <head>	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">	<meta http-equiv="Pragma" content="no-cache">	<meta http-equiv="Expires" content="-1">	<meta http-equiv="Cache-Control" content="no-cache">	<title><?php echo $config['lms_title']; ?></title>	<link rel="stylesheet" type="text/css" href="ext/resources/css/ext-all.css">	<link rel="stylesheet" type="text/css" href="ext/resources/css/xtheme-slate.css">	<link rel="stylesheet" type="text/css" href="include/main.css">	<link rel="stylesheet" type="text/css" href="custom/lms.css">	<style type="text/css">	#loading-mask{	  position:absolute;	  left:0;	  top:0;	  width:100%;	  height:100%;	  z-index:20000;	  background-color:white;	  display: none;	}	#loading{	  position:absolute;	  left:45%;	  top:40%;	  padding:2px;	  z-index:20001;	  height:auto;	  display: none;	}	#loading a {	  color:#225588;	}	#loading .loading-indicator{	  background:white;	  color:#444;	  font:bold 13px tahoma,arial,helvetica;	  padding:10px;	  margin:0;	  height:auto;	}	#loading-msg {	  float: left;	  margin-top: 8px;	  font:bold 13px tahoma,arial,helvetica;	}	#loading-status {	  float: left;	  font:normal 11px tahoma,arial,helvetica;	}	#downloadTarget {	  width: 1px;	  height: 1px;	  position: absolute;	  top: -10px;	  left: -10px;	}	</style>  </head>  <body>	<div id="require">The LMS requires that your browser has Javascript support enabled.<br><br>Loading required files, please stand by...<br><br>If this message does not disappear, please ensure that your browser is not blocking Javascript execution.</div>	<script type="text/javascript">	document.getElementById("require").style.display = "none";	</script>	<div id="loading-mask" style=""></div>	<div id="loading">	  <div class="loading-indicator"><img src="images/loading.gif" style="margin-right:8px; float:left; vertical-align:top; width: 32px; height: 32px;"><div id="loading-msg">Loading...<br><div id="loading-status"></div></div></div>	</div>	<script type="text/javascript">	document.getElementById("loading-mask").style.display = "block";	document.getElementById("loading").style.display = "block";	</script>	<script type="text/javascript">document.getElementById("loading-status").innerHTML = "Loading base components";</script>	<script type="text/javascript" src="ext/adapter/ext/ext-base.js"></script>	<script type="text/javascript">document.getElementById("loading-status").innerHTML = "Loading core API";</script>	<script type="text/javascript" src="ext/ext-all<?php if (LMS_DEBUG) echo '-debug'; ?>.js"></script>	<script type="text/javascript">document.getElementById("loading-status").innerHTML = "Loading extensions";</script>	<script type="text/javascript" src="include/patch<?php if (LMS_DEBUG) echo '-debug'; ?>.js"></script>	<script type="text/javascript">document.getElementById("loading-status").innerHTML = "Loading LMS base";</script>	<script type="text/javascript" src="include/lms<?php if (LMS_DEBUG) echo '-debug'; ?>.js"></script>	<script type="text/javascript" src="include/language.js"></script>	<script type="text/javascript">document.getElementById("loading-status").innerHTML = "Initializing";</script>	<script type="text/javascript" src="include/pw-reset.js"></script>	<script type="text/javascript">	Ext.onReady(TC.lms.init, TC.lms);	</script>		<script type="text/javascript">	<?php	if ($token == '')	{	  echo "var token_blank = true;\n";	}	else	{	  echo "var token_blank = false;\n";	  $db->sql('SELECT id, pw_reset_time, active FROM users WHERE pw_reset_token=%s');	  $db->add_param($token);	  $user = $db->select();	  if ($user)	  {		echo "var user_found = true;\n";		if (time() - $user[0]['pw_reset_time'] < (24 * 60 * 60))		{		  echo "var time_limit_reached = false;\n";		  if ($user[0]['active'])		  {			echo "var user_active = true;\n";			echo "var reset_user_id = \"{$token}\";\n";		  }		  else		  {			echo "var user_active = false;\n";		  }		}		else		{		  echo "var time_limit_reached = true;\n";		}	  }	  else	  {		echo "var user_found = false;\n";	  }	}	?>	</script>  </body></html>

So all that does is include a bunch of Javascript files and define a couple variables. The pw-reset.js file looks like this:

TC.lms.startup = function(){  Ext.getDom("title_banner").style.display = "none";  var ajax_con = new Ext.data.Connection({ url: "io.php", method: "post"});  var submit_reset = function()  {	var reset_frm = Ext.getCmp("reset_form");	if (reset_frm.getForm().isValid())	  reset_frm.getForm().submit({		method: "POST",		waitTitle: "Connecting",		waitMsg: "Sending Data...",		url: "io.php",		scope: this,		params: {page_mode: "reset_password"},		success: function(frm, act)		{		  Ext.Msg.alert('Information', act.result.info);		},		failure: this.submit_failure	  });	else	{	  Ext.Msg.alert("Warning", "Please fill out all required fields.");	}  }    var do_reset_password = function()  {	var pass_frm = Ext.getCmp("pass_form");	if (pass_frm.getForm().isValid())	  pass_frm.getForm().submit({		method: "POST",		waitTitle: "Connecting",		waitMsg: "Sending Data...",		url: "io.php",		scope: this,		params: {page_mode: "change_password"},		success: function(frm, act)		{		  Ext.Msg.alert('Information', act.result.info, function()			{			  window.location.href = "index.php";			}		  );		},		failure: this.submit_failure	  });	else	{	  Ext.Msg.alert("Warning", "Please fill out all required fields.");	}  }  var preload_complete = function()  {	var pass_frm = new Ext.FormPanel({	  labelWidth: 100,	  labelAlign: "right",	  bodyStyle: "background: transparent; margin-bottom: 10px;",	  border: false,	  autoHeight: true,	  anchor: "100%",	  url: "io.php",	  id: "pass_form",	  defaults: {		xtype: "textfield",		allowBlank: false,		width: 150	  },	  items: [		{		  xtype: "panel",		  border: false,		  anchor: "100%",		  bodyStyle: "background: transparent; margin-bottom: 20px;",		  html: "<div>Enter your new password below.</div>"		},		{		  id: "reset_password",		  name: "password",		  inputType: "password",		  fieldLabel: "New Password"		},		{		  id: "reset_conf",		  name: "conf",		  inputType: "password",		  fieldLabel: "Confirm Password"		},		{		  id: "reset_id",		  name: "uid",		  inputType: "hidden",		  allowBlank: true,		  hideLabel: true		}	  ],	  buttons: [		{		  text: "Reset Password",		  formBind: true,		  scope: this,		  handler: do_reset_password		}	  ]	});	var reset_frm = new Ext.FormPanel({	  labelWidth: 100,	  labelAlign: "right",	  bodyStyle: "background: transparent; margin-bottom: 10px;",	  border: false,	  autoHeight: true,	  anchor: "100%",	  url: "io.php",	  id: "reset_form",	  defaults: {		xtype: "textfield",		width: 150	  },	  items: [		{		  xtype: "panel",		  border: false,		  anchor: "100%",		  bodyStyle: "background: transparent; margin-bottom: 10px;",		  html: "<div>To reset your password, enter the email address that you registered with.</div>"		},		{		  id: "reset_email",		  fieldLabel: "Email Address",		  name: "email"		},		{		  xtype: "panel",		  border: false,		  anchor: "100%",		  bodyStyle: "background: transparent; margin-bottom: 10px; margin-top: 20px;",		  html: "<div>If you don't know which email address you registered with, you may also enter your username.</div>"		},		{		  id: "reset_username",		  fieldLabel: "Username",		  name: "username"		}	  ],	  buttons: [		{		  text: "Reset",		  formBind: true,		  scope: this,		  handler: submit_reset		}	  ]	});	var login = new Ext.Window({	  bodyStyle: "padding: 10px",	  closable: false,	  height: 350,	  width: 300,	  id: "login_win",	  onEsc: Ext.emptyFn,	  frame: true,	  title: "Log In",	  layout: "fit",	  items: [		{		  id: "login_content",		  layout: "card",		  border: false,		  bodyStyle: "background: transparent;",		  autoScroll: true,		  activeItem: 0,		  items: [			pass_frm,			reset_frm		  ]		}	  ]	});	if (token_blank)	{	  login.show();	  login.center();	  Ext.getCmp("login_content").getLayout().setActiveItem("reset_form");	  Ext.Msg.alert("Error", "A user was not found to reset a password.  Fill out the form to generate a new email to reset your password.");	}	else	{	  if (!user_found)	  {		login.show();		login.center();		Ext.getCmp("login_content").getLayout().setActiveItem("reset_form");		Ext.Msg.alert("Error", "A user was not found that corresponded to the information provided.  Fill out the form to generate a new email to reset your password.");	  }	  else	  {		if (time_limit_reached)		{		  login.show();		  login.center();		  Ext.getCmp("login_content").getLayout().setActiveItem("reset_form");		  Ext.Msg.alert("Error", "The 24-hour time limit for resetting your password has expired.  Fill out the form to generate a new email to reset your password.");		}		else		{		  if (!user_active)		  {			Ext.Msg.alert("Error", "The account you are trying to reset a password for is not active.  An administrator must activate the account before you can change your password.");		  }		  else		  {			login.show();			login.center();			Ext.getCmp("reset_id").setValue(reset_user_id);		  }		}	  }	}  }  this.load_options(preload_complete); // load LMS options}

So all of that is pure Javascript, there's no PHP or anything else there.Sure, there are two languages to learn, but each is for a distinctly different part. There's not really any overlap between them other than using PHP to write out some Javascript variables. The danger of a beginner learning Javascript for everything is that they might not be able to separate the two pieces, if later on they wanted to learn PHP they might try to replace the client-side code with PHP.

Edited by justsomeguy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...