Sanjay Kelkar
Sanjay Kelkar

Reputation: 1

How to send a value from a html page to php page with text validation

After searching for my stuff, I didn't found much usefull info on other Websites or forums.So decided to ask a straight question here. I have a html file which checks the field is empty or not, if empty it gives a alert, eyerthing is fine uptill here.My question is how to send the text value to the php page if the text box is not empty.My passwordres.html file goes like this:

<link href="style.css" rel="stylesheet" type="text/css">
<form onsubmit="return checkvalue()" method="post">
<ul class=mainForm id="mainForm_1"> 
<li class="mainForm" >Enter your Email address :</li>  
<input name="FNAME" type="text" id="mystring"><br>
<br>
<input type="submit" value="Proccedd" />
</form></ul></div>
<script lang="javascript">
 function checkvalue() { 
 var a = document.getElementById('mystring').value;
 if(!(a).match(/\S/)) {
    alert ('Empty value is not allowed');
    return true;
}
else
 {
 window.open("http://...Secured.php?mystring="//value here""); 
return false;}
 }
</script>

Any help would be greatfull.Thanks in advance.

Upvotes: 0

Views: 347

Answers (4)

vusan
vusan

Reputation: 5331

Concatenate the a variable to your url like:

window.open("http://...Secured.php?mystring="+a);

It will easily get by Secured.php by:

$_GET['mystring']

Edit:
This is not right way to pass string from form. return true will allow the form to submit and you will get the value by $_POST['FNAME']. You also need to validate that in server side.

Upvotes: 1

Nitesh Mishra
Nitesh Mishra

Reputation: 311

instead of doing window.open. you can simply return false if field is empty else return true. and in form add attribute action with php page name. Something like this.

<link href="style.css" rel="stylesheet" type="text/css">
<form action="http://...Secured.php" onsubmit="return checkvalue()" method="post">
<ul class=mainForm id="mainForm_1"> 
<li class="mainForm" >Enter your Email address :</li>  
<input name="FNAME" type="text" id="mystring"><br>
<br>
<input type="submit" value="Proccedd" />
</form></ul></div>

<script type="text/javascript">
function checkvalue() { 
 var a = document.getElementById('mystring').value;
 if(a=="" || a==null) {
    alert ('Empty value is not allowed');
    return false;
}
else
 {
 return true;
 }
 }
</script>

Upvotes: 0

Roy M J
Roy M J

Reputation: 6948

Use jquery ajax.

 $('#submit').click(function(){
   name  =$("#name").val();
   if(name==""){
      alert("Name Required!");
   }else{
      $.ajax({
         url:'page.php',
         data:'name='+name,
         success(result){
           alert(result);
         }
      });
   }
 });

You need to include jquery library in your html page.

Cheers

Upvotes: 1

Godea Andrei
Godea Andrei

Reputation: 55

I think you should send data from your form where you want using this

<form action="http://...Secured.php" onsubmit="return checkvalue()" method="post">

instead of this one

<form onsubmit="return checkvalue()" method="post">

And there you can refer to a variable like this

$_POST['variable_name_from_form']

Upvotes: 0

Related Questions