Jump to content

printing message in the input box


bindiya

Recommended Posts

I have an html form for subscribing to newsletter.

I use ajax and php for (1)accepting the value and (2) submitting to the database and (3) printing the success messsage back.

I use a <div> to print the success message. but the client need the message to be printed in the input box where i used to enter the email.

This is not working for me.I am pasting the code  below.

/*------the ajax and email validation*/

scriptmail.html
<script>
var xmlHTTP;

function sendmail(email)
{
if(window.XMLHttpRequest)
{
	xmlHTTP = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
	xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
}

if(xmlHTTP==null)
{
	alert("Sorry You Browser No Supports XMLHTTP");
	return;
}
var url;
url = "mail.php?email="+email;
xmlHTTP.onreadystatechange=state;
xmlHTTP.open("GET",url,true);
xmlHTTP.send(null);
}

function state()
{
var Message = document.getElementById('div_msg');
var str;
if(xmlHTTP.readyState==4)
{
		str = xmlHTTP.responseText;		
}
else
{
	str = "";
}
Message.className = 'divclass';
Message.innerHTML = str;
}
<!-- end mail.js file --></script>
<script type="text/javascript">
function send()
{

var email = document.getElementById("email_id").value;
//alert(email);
sendmail(email);	
}
</script>
<script language="javascript">
function verify_email(){//email_subscription
var email=document.getElementById("email_id").value;

var filter  = /^([a-zA-Z0-9_ \.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(email == '')
   {
     alert("Enter email for subscribing newsletter,Thanks.");
 document.getElementById('email_id').value='';
 document.getElementById("email_id").focus();
 return false;
   }

else if (!filter.test(email))
{
	alert('Incorrect email address');
	document.getElementById('email_id').value='';
	document.getElementById('email_id').focus();
	return false;
}
else return true;
}

</script>
<style type="text/css">
<!--//#FF0000;

.divclass {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: black; 
/*background-color: #FFCC00;
border: 1px solid #009900;*/
}
-->
</style>
<form name='email_subscription' id='email_subscription' action='' method='get' onsubmit="return verify_email();" >
                          
<table  bordercolor="#660000" border="1" frame="box" rules="none" >
                               
                             
                           
                          <tr><td colspan='2'><p class="style28"><strong><span class="style22">Subscribe to our Newsletter</a></span></strong></p></td></tr>
                           <tr><td colspan="2" ><div id="div_msg"></div></td></tr> 
                          <tr><td width="42"><p class="rvts16 style4 style14">   Email</p></td><td width="212"><input type='text' id='email_id' name='email_id' size="25" /></td>
                          </tr>
                          <tr>
                            <td height="18" colspan="2" style='text-align:right'><button type="button" id="btnEmail" onClick="send();">Send Email</button><!--<input type='submit' value=Subscribe name='subscribe' id='subscribe'  onclick="send();"/>--></td></tr>
                          </table></form>
/*--the php part*/
mail.php
<?php
include('config.php');

if(isset($_GET['email'])) 
{
$email = $_GET['email'];
$date_time   = date('Y-m-d');
$query="insert into newsletter_emails(email_id,date_subscribed) values('$email','$date_time')";
$email=mysql_query($query);
if($email)
{
echo "Your email sent successfully";
}
else
{
echo "Error sending email";
}
}
else
{
echo "Please fill all fields";
}
?>


 

Link to comment
https://forums.phpfreaks.com/topic/232732-printing-message-in-the-input-box/
Share on other sites

still the problem is there i have changed the code a lot.

Now in invalid message gets printed in the input box but the success message doesnot print.

can anyone help

mail.php
$query1="select count(*) from  newsletter_emails";
	$c=mysql_query($query1);

	$old_count=mysql_num_rows($c);

	$email = $_GET['email'];$date_time   = date('Y-m-d');

if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email))
{ 


		$query="insert into newsletter_emails(email_id,date_subscribed) values('$email','$date_time')";
		$email1=mysql_query($query);
		echo "OK";
}

else{
	echo "INVALID E-MAIL";
		}



==========html
<script>
var xmlHTTP;

function sendmail(email)
{
if(window.XMLHttpRequest)
{
	xmlHTTP = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
	xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
}

if(xmlHTTP==null)
{
	alert("Sorry You Browser No Supports XMLHTTP");
	return;
}
var url;
url = "email_subscription11.php?email="+email;
//alert(url);
xmlHTTP.onreadystatechange=state;
xmlHTTP.open("GET",url,true);
xmlHTTP.send(null);
}

function state()
{
if(xmlHTTP.readyState==4)
{
		alert  (xmlHTTP.responseText);
		document.getElementById("email_id").value = xmlHTTP.responseText;//alert  (xmlHTTP.responseText);
}
}
</script>
<script type="text/javascript">
function send()
{

var email = document.getElementById("email_id").value;
//alert(email);
sendmail(email);	
}
</script>
<script language="javascript">
function verify_email(){
var email=document.getElementById('email_id').value;
//alert(email);
var filter  = /^([a-zA-Z0-9_ \.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(email == '')
   {
     alert("Enter email for subscribing newsletter.");
 document.getElementById('email_id').value='';
 document.getElementById("email_id").focus();
 return false;
   }

else if (!filter.test(email))
{
	alert('Incorrect email address');
	document.getElementById('email_id').value='';
	document.getElementById('email_id').focus();
	return false;
}
else return true;
}

</script>

<form name='email_subscription' id='email_subscription' action='' method='get' onsubmit="return verify_email();"  >

                          <table width="178"  align="right">
                                                  
                          <tr><td width="108"   style="text-align:right"><input type='text' id='email_id' name='email_id' size="18" value="Your Email" style ="COLOR: #999999;" onclick="change_value(this);" /></td><td width="72"><input type='submit' value='Sign Up' name='subscribe' id='subscribe' onClick="send();" /></td></tr>
                         </table></form>

 

but still the success message "OK" is not displayed in the input box"email_id"

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.