Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have four text boxes name
textbox1:
textbox2:
textbox3:
textbox3:
I need to validate this field by following some criteria like below :
1.If anyone put value in textbox1,he must forced to put value in textbox2.Otherwise both textbox can be empty.
2.If anyone put value in textbox3,he must forced to put value in textbox4.Otherwise both textbox can be empty.
Above this criteria,how can i make validation.
You can try this.May be it will help you.
first put the css and js library.
<link rel="stylesheet" href="http://www.position-relative.net/creation/formValidator/css/validationEngine.jquery.css" type="text/css"/>
<script src="http://www.position-relative.net/creation/formValidator/js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="http://www.position-relative.net/creation/formValidator/js/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>
<script src="http://www.position-relative.net/creation/formValidator/js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
this is your js function which will validate your form field while submit the form
<script type="text/javascript">
$(function(){
$("#ckForm").validationEngine();
});
</script>
Now the form
<form action="" method="post" accept-charset="utf-8" id="ckForm">
<input type="text" name="textbox1" id="textbox1" />
<input type="text" name="textbox2" id="textbox2" class="validate[condRequired[textbox1]]" />
<input type="text" name="textbox3" id="textbox3" />
<input type="text" name="textbox4" id="textbox4" class="validate[condRequired[textbox3]]" />
<input type="submit" value="GO" />
</form>
Please let me know if you need any further help.
It's just an example and not a final solution but if you've a few PHP and JS skills it should help you a lot.
PHP:
$Input = new Input();
$Input
->post("username")
->validate(Input::RULE_MINLENGTH, 4)
->validate(Input::RULE_MAXLENGTH, 20)
->post("email")
->validate(Input::RULE_NOT_EMPTY, null)
->validate(Input::RULE_IS_EMAIL, true)
->post("email2")
->validate(Input::RULE_EQUALS, "email")
->post("password")
->validate(Input::RULE_MINLENGTH, 1)
->validate(Input::RULE_MAXLENGTH, 20)
->post("password2")
->validate(Input::RULE_EQUALS, "password")
->post("random_text");
if (empty($_POST)) {
// $_POST is empty, show the template!
$Template->assign("validation_rules", $Input->get_rules_json());
$Template->show("template.tpl");
} else {
if ($Input->submit("post")) {
// everything alright
echo "No errors.";
} else {
echo "Errors: ";
print_r($Input->get_errors());
}
}
Template/JavaScript:
$("#form").ajaxForm({
"beforeSubmit": function() {
// validate
return formValidation($("#form"), '<?php echo $this->validation_rules?>');
}
});
Edit:
Check out this video: http://www.youtube.com/watch?v=Qf6o3MIcAtA
Enjoy!
I think what you are trying to say to me it seems as if you require ajax. See if you can use AJAX or not for fulfilling your requirement.
You can achive this using simple java script function.
function validate()
{
var txtBox1= document.getElementById('textbox1');
var txtBox2= document.getElementById('textbox2');
var txtBox3= document.getElementById('textbox3');
var txtBox4= document.getElementById('textbox3');
if(txtBox1.value!=""&&txtBox2.value=="")
{
alert("Enter a value in text box");
txtBox2.focus();
return;
}
if(txtBox3.value!=""&&txtBox4.value=="")
{
alert("Enter a value in text box");
txtBox4.focus();
return;
}
}
you can call this method onchange of each text box.let me know i answer your question?
Related
I'm trying to pass a variable from an html file, using jquery and ajax to a php file, where I want to process it. I have two test files:
ajax.html
<html>
<head>
<title>ajax</title>
<script type="text/javascript" src="jquery.js"></script>
<link href="ajax.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function () {
$("#call_back").click(function () {
$.post(
"ajax.php", {
value: $("#input_text").val()
},
function (data) {
$("#response_text").val(data);
}
);
});
});
</script>
</head>
<body>
<div id="wrapper">
<h3>Ajax</h3>
<div class="entry-wrapper">
<h4>Data to send to the server</h4>
<input type="text" size="50" id="input_text" />
<input type="button" value="Ajax Callback" id="call_back" />
</div>
<div id="response_wrapper">
<textarea id="response_text"></textarea>
</div>
</div>
</body>
</html>
and ajax.php
<?php
$value = $_POST['value'];
echo "Returned from the server: $value";
?>
The html works fine. If I enter a value in the text field, it's passed to the php file, processed and sent back to be displayed in the textarea.
I've looked in firebug and the post tag in the console shows the values that I enter.
The problem is that I want to be able to use the variable in the php file, for arithmetic operations or running an sql query with it, but it shows up as undefined index when I open the php file.
I've looked through multiple threads on passing variables from jquery to php but still found no answer. I have no idea what the problem is. Suggestions are much appreciated. Thanks.
If I understand you question correctly, you need to define the variable in your url.
Example:
http://yoursite.com/ajax.php?value=yourdata
This way when you open the file you are defining the value which is all you are doing with your ajax request. If this is not what you are trying to accomplish, try to rephrase your question.
Revised after 1st comment:
<?php
if(isset($_POST['value'])){
//do whatever in here
dowhatever();
}
function dowhatever() {
$value = $_POST['value'];
echo "Returned from the server: $value";
if(value == 'data') {
//run query
}//end if
}
?>
I assume that when you say you can confirm it's working, but when you directly open the .php file, it has undefined index, then I would say that makes perfect sense. If you are browsing directly to the .php file, then no POST data is being sent. But if you are able to type in some text, fire off the submit, and get text back as planned, then it's working.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Ok I can't get this jQuery to work whatsoever! And its so simple! All I want it to do is make the #main div fade in on load.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="randomizer.js"></script>
<title>
<?php
if (isset($_POST['first']) && isset($_POST['second'])) {
echo "Randomized {$_POST['first']}-{$_POST['second']}";
}
else {
echo "Randomizer";
}
?>
</title>
<link type="text/css" rel="stylesheet" href="randomizer.css" />
<script type="text/javascript" src="randomizer.js"></script>
</head>
<body>
<?php
if (isset($_POST['first']) && isset($_POST['second'])) {
$grabFirst = strip_tags($_POST['first']);
$grabSecond = strip_tags($_POST['second']);
}
?>
<div id="main">
<center><form action="randomizer.php" method="post">
<input type="text" name="first" value="<?php if(isset($grabFirst)){ echo htmlspecialchars($grabFirst); } ?>" placeholder="First Number" /><br />
<input type="text" name="second" value="<?php if(isset($grabSecond)){ echo htmlspecialchars($grabSecond); } ?>" placeholder="Second Number" /><br />
<input type="submit" />
</form>
<?php
if (isset($_POST['first']) && isset($_POST['second'])) {
$grabFirst = strip_tags($_POST['first']);
$grabSecond = strip_tags($_POST['second']);
}
else {
print "Please enter two numbers.\n";
die;
}
if (strlen($grabFirst) < 1 or strlen($grabSecond) < 1) {
print "Please make sure you filled out both feilds.\n";
die;
}
elseif (!is_numeric($grabFirst) && !is_numeric($grabSecond)) {
print "Please make sure you entered two valid numbers.\n";
die;
}
elseif (!is_numeric($grabSecond)) {
print "Please make sure you entered two valid numbers.\n";
die;
}
elseif (!is_numeric($grabFirst)) {
print "Please make sure you entered two valid numbers.\n";
die;
}
else {
}
$rand = rand($grabFirst, $grabSecond);
print "A random number between {$grabFirst} and {$grabSecond} is {$rand}.\n";
?></center>
</div>
</body>
</html>
Heres my jQuery
$(document).ready(function(){
$('#main').fadeTo('slow', 1);
});
If anyone could help, I'd really appreciate it. I've been trying to fix it for like an hour, and I have no luck. I'm just beginning with jQuery and have no experience, maybe its a really dumb error, but I have no idea. Thanks~
You need to include jQuery in your page:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
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.
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
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.
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']
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>
My code is as follows:
<html>
<input type="datetime" name="datepicker" id="datepicker" />
<P id=firstp></P>
<button id="button" onClick="test_function();" type="button">View</button>
<script type="text/javascript">
function test_function(){
$("#firstp").load("view_calls_sort.inc.php?datepicker="+ $("#datepicker").val());}
</script>
</html>
view_calls_sort.inc.php:
$datepicker=$_GET['datepicker'];
echo $datepicker;
im trying to get the datepicker value to view_calls_sort.inc.php, Above code doesn't seem to work,as an example when I input datetime like this
2005-04-19 12:00:00.000 it is not shown in view_calls_sort.inc.php, Can any one help me with this. Thanks
Try encoding it:
var value = $("#datepicker").val();
$("#firstp").load("view_calls_sort.inc.php", { datepicker: value });
or:
$("#firstp").load("view_calls_sort.inc.php?datepicker=" + encodeURIComponent(value));
Also make sure that your script tag is inside your <html> tag and not as it is currently shown in your question.
First, make sure that the function is called.
<script type="text/javascript">
function test_function(){
alert($("#datepicker").val());
$("#firstp").load(
"view_calls_sort.inc.php?datepicker="+$("#datepicker").val());
}
</script>
If the alert shows up, the method is called. And you get to see which value is sent to the server.
You also could change the php script and have it return a fixed value (echo 'Foo';) so you can see if the script is called.
Hiya:
i know some people would be so tired of my questions, but I'm working on a uni project and need to get it done as soon as possible. This question is about using JS on a button(button) and sending a php_my_sql update on the same button. The problem is JS uses button, right? but PHP uses button(submit). How can I get these two to work on one of these buttons, cuz there has to be only one button.
this is my code for JS
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect")
x.remove(x.selectedIndex)
}
</script>
HTML
<form method="post">
<select id="collect" name="Select1" style="width: 193px">
<option>guns</option>
<option>knife</option>
</select> <input type="**submit/button**" onclick="formAction()" name="Collect" value="Collect" /></form>
PHP
<?
if (isset($_POST['Collect'])) {
mysql_query("UPDATE Player SET score = score+10
WHERE name = 'Rob Jackson' AND rank = 'Lieutenant'");
}
?>
This can be a way
Submit the form through JS after removing parameter
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect")
x.remove(x.selectedIndex);
document.forms[0].submit();
}
</script>
Input type button
<input type="button" onclick="formAction()" name="Collect" value="Collect" />
Embed jQuery and use $.post() to send an AJAX request.
JavaScript can interact with the button whilst the user is navigating the page and entering data into the form. The instant the user pushes the submit button and the request for the form submission is sent JS no longer has control. The request is sent to the form's action (most likely a PHP file) which processes the request and gives an answer back.
If you really need to combine the two, look into AJAX.
<?php print_r($_POST); ?>
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect");
x.remove(x.selectedIndex);
submit_form();
}
function submit_form() {
document.form1.submit();
}
</script>
<form method="post" name='form1'>
<input type='hidden' name='Collect'/>
<select id="collect" name="Select1" style="width: 193px">
<option>guns</option>
<option>knife</option>
</select> <input type="button" onclick="formAction()" name="Collect" value="Collect" /></form>
<?
if (isset($_POST['Collect'])) {
//do whatever update you want
}
?>
Simple Solution
Make this modification in the form tag
<form method="post" onsubmit="return formAction()">
In JavaScript function add a line "return true;" at the end of the function.
Voila ..!!! you are done..!!
Enjoy..!!