<script type="text/javascript">
function myFunction(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();}
</script>
<form id="form1" action="php/create.php">
Name </br><input type="text" name="inputName" value="" id=""> </input>
Hemsida </br><input type="text" name="inputPage" value="http://" id=""> </input>
<input type="button" onclick="return myFunction()" value="Submit"/>
</form>
<form id="form2" action="php/createhemerb.php">
<input type="text" name="inputFon" value="" id="fnid" hidden/>
<input type="text" name="inputJob" value="" id="fnid" hidden/>
</form>
I reach out to the php files but the both come out "please fill out the form 1" and "please fill out the form 2"
if(!$_POST['submit']) {
echo "please fill out the form";
header ('Location: please fill out the form 1');}
I have tried things back and fourth for hours i can not get it to work.
Please do the following to fix your issue:
Add method='post' to your forms
If you intend to check the value of $_POST['submit'], you need to name your button 'submit'.
You can't use echo and then header(''), it will set a 'header already sent exception'.
header('Location: some text') has no meaning, header('Location: file.html') is the correct syntax.
Full code:
<script type="text/javascript">
function myFunction(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
}
</script>
<form id="form1" action="php/create.php" method="post">
Name </br><input type="text" name="inputName" value="" id=""> </input>
Hemsida </br><input type="text" name="inputPage" value="http://" id=""> </input>
<input type="button" onclick="return myFunction()" value="Submit" name="submit"/>
</form>
<form id="form2" action="php/createhemerb.php" method="post">
<input type="text" name="inputFon" value="" id="fnid" hidden/>
<input type="text" name="inputJob" value="" id="fnid" hidden/>
</form>
PHP:
if(empty($_POST) || !$_POST['submit']) {
echo "please fill out the form";
//you can't set the header after echoing
//header ('Location: please fill out the form 1');//use header('Location: error.html') instead.
//or output a Javascript redirect echo "<script>window.location = 'error.html';</script>";
}
Hope this helps!
Related
I've tried the following code for javascript validation. When the form is submitted, only the first input field is validated. How could I validate all the dynamic values of the form?
<script type="text/javascript">
function validate() {
if (document.f.phone.value.length == "0") {
alert("Phone is required");
document.f.phone.focus();
return false;
} else if (document.f.file.value.length == "0") {
alert("file is required ");
document.f.file.focus();
return false;
}
return true;
}
</script>
<?php
echo '<form name="f" action="" method="post" enctype="multipart/form-data" onsubmit="return validate();">
<input type="text" name="phone[]" class="required">
<input type="file" name="file[]" class="required">
<input type="button" class="add" value="add"/>
<input type="submit" value="Submit" name="submit" class="Submit">
';
?>
if i understood correctly then you want to validate dynamic value in the form. instead of writing logic to validate the fields just add the required keyword to the fields which you want to make mandatory. that should save you extra effort of writing validation. go for custom validation when inbuilt validation are not enough
<form name="f" action="" method="post" enctype="multipart/form-data">
<input type="text" name="phone[]" required class="required">
<input type="file" name="file[]" required class="required">
<input type="button" class="add" value="add" />
<input type="submit" value="Submit" name="submit" class="Submit">
I want to reset the text area on my submit which sends data without changing the page.
<iframe name="votar" style="display:none;"></iframe>
<form id ="myForm" action="message-process.php" method="post" target="votar">
<?php
echo "<input type='hidden' name='userId' value= '$sendId'>";
?>
<textarea rows="5" cols="60" name="message" type="text" maxlength="255" style="font-size:10px background-color:grey;"></textarea>
<input type="submit" value="Send Message">
</form>
You can use HTML form attribute onsubmit="func()"
And inside a Javascript script set the value of what you wish to an empty string.
This code works.
<iframe name="votar" style="display:none;"></iframe>
<form id ="myForm" action="message-process.php" method="post" target="votar" onsubmit="this.submit(); this.reset(); return false;">
<?php
echo "<input type='hidden' name='userId' value= '$sendId'>";
?>
<textarea id ='input' rows="5" cols="60" name="message" type="text" maxlength="255" style="font-size:10px background-color:grey;"></textarea>
<input type="submit" value="Send Message">
</form>
Am trying to get a value from a different form to store in a session. When the user clicks on Calculate on form one it opens form two.In form two the user enters a value in a textfield. Now incase the user presses the calculate button again I would want the page to store the values in form two in a session so that when form two re appears the value is stored.
is there a way to use javascript to store it in a php session
<?php
if(isset($_POST['calculate']))
{
$_SESSION['ss'] = '<script>doument.form[frm2].ss.value</script>';
}
?>
<form action="" method="post" name="frm1" id = "frm1" enctype="multipart/form-data" onkeypress="return event.keyCode != 13;">
<input type="text" name="id" value="<?php echo $id; ?>" />
<input type="submit" name="calculate" id="calculate">
<form>
<?php
if(isset($_POST['calculate'])){
?> ?>
<form action="go.php" method="post" name="frm2" id = "frm1" enctype="multipart/form-data" onkeypress="return event.keyCode != 13;">
<input type="text" name="ss" value="" />
<input type="submit" name="submit" id="submit">
<form>
<?php
}
?>
If you are using jQuery can try this way .
first define a hidden field in form 1
<form action="" method="post" name="frm1" id = "frm1" enctype="multipart/form-data" onkeypress="return event.keyCode != 13;">
<input type="text" name="id" value="<?php echo $id; ?>" />
<input type="hidden" id="hidden_ss" name="hidden_ss" value=""/>
<input type="submit" name="calculate" id="calculate">
</form>
Then in jQuery when you change the frm2 -> ss value add the value to hidden field
$("input[name='ss']").change(function(){
$("#hidden_ss").val($(this).val());
});
then wen you create calculate button
<?php
$ss = trim($_POST['hidden_ss']);
?>
<form action="" method="post" name="frm1" id = "frm1" enctype="multipart/form-data" onkeypress="return event.keyCode != 13;">
<input type="text" name="id" value="<?php echo $id; ?>" />
<input type="hidden" id="hidden_ss" name="hidden_ss" value=""/>
<input type="submit" name="calculate" id="calculate">
</form>
<?php
if(isset($_POST['calculate'])){
?> ?>
<form action="go.php" method="post" name="frm2" id = "frm1" enctype="multipart/form-data" onkeypress="return event.keyCode != 13;">
<input type="text" name="ss" value="<?php echo $ss; ?>" />
<input type="submit" name="submit" id="submit">
</form>
<?php
}
?>
I believe you don't need JavaScript for this.
Just something like this:
<form action="" method="post" name="frm1" id = "frm1" enctype="multipart/form-data" onkeypress="return event.keyCode != 13;">
<input type="text" name="id" value="<?=htmlentities($_SESSION['id'])?>" />
<input type="submit" name="calculate" id="calculate">
</form>
<?php
if(isset($_POST['calculate']))
{
$_SESSION['id'] = $_POST['id'];
?>
<form action="go.php" method="post" name="frm2" id = "frm1" enctype="multipart/form-data" onkeypress="return event.keyCode != 13;">
<input type="text" name="ss" value="<?=htmlentities($_SESSION['id'])?>" />
<input type="submit" name="submit" id="submit">
</form>
<?php
}
?>
I m trying to show/hide textarea when clicked on particular radio button.I need to validate the textarea such that user has to enter details in the textarea.I m trying with 3 radio buttons when user clicks on 3rd radio button which is Other textarea should be displayed and it should be validated such that the user fills the textarea whereas for rest 2 radio buttons textarea should not be displayed. How can I do this?
Here is the code
<script type="text/javascript">
$(function() {
$("input[type='radio']").change(function(){
if($(this).val()=="Other, please give details")
{
$("#otherAnswer").show();
}
else
{
$("#otherAnswer").hide();
}
});
});
</script>
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[_a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "The e-mail address you entered is not valid. <br/>";
}
} else {
$error .= "You didn't type in an e-mail address. <br />";
}
if (empty($_POST["mail"])) {
$error = "Reason is required";
} else {
$mail = $_POST["mail"];
}
<form action="" method="post" style="margin-top:20px;">
<input type="hidden" name="subject" value="Form Submission" />
<input type="hidden" name="redirect" value="" />
<label style="font-size:14px;">E-mail:</label>
<input type="text" name="email" value="" style="margin-left:30px;"<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" />
<br/>
<br/>
<label >
<input name="mail" type="radio" id="mail" value="I receive too many e-mails" />I receive too many e-mails<br /></label>
<label ><input name="mail" id="mail1" type="radio" value="I don’t find the e-mails interesting or useful" />I don’t find the e-mails interesting or useful</label>
<br/>
<label >
<input name="mail" type="radio" id="mail2" value="Other, please give details" />Other, please give details:</label><br/>
<textarea style="display:none;" type="text" name="otherAnswer" id="otherAnswer"/></textarea>
<input type="submit" class="submit" name="submit" value="SEND" />
</form>
JS Code:
<script>
$(document).ready(function() {
$("#otherAnswer").hide();
$(".opt").change(function(){
var id=$(this).attr("id");
if(id=="mail2")
{
$("#otherAnswer").show();
}
else
{
$("#otherAnswer").hide();
}
});
$("#feedbackform").submit(function(){
var selected_val=$(".opt:checked").val();
if(selected_val=="other_reason")
{
if($("#otherAnswer").val()=="")
{
alert("please enter reason");
// you can display message next to text area.
return false;
}
}
})
});
</script>
HTML code:
<form action="" method="post" style="margin-top:20px;" id="feedbackform">
<input type="hidden" name="subject" value="Form Submission" />
<input type="hidden" name="redirect" value="" />
<label style="font-size:14px;">E-mail:</label>
<input type="text" name="email" value="" style="margin-left:30px;"<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" />
<br/>
<label >
<input name="mail" type="radio" id="mail" value="I receive too many e-mails" class="opt"/>I receive too many e-mails<br /></label>
<label ><input name="mail" id="mail1" type="radio" value="I don’t find the e-mails interesting or useful" class="opt"/>I don’t find the e-mails interesting or useful</label>
<br/>
<label >
<input name="mail" type="radio" id="mail2" value="other_reason" class="opt"/>Other, please give details:</label><br/>
<textarea type="text" name="answer" id="otherAnswer"/></textarea>
<input type="submit" class="submit" name="submit" value="SEND" />
</form>
solved both show hide and validation problem.
I've given class to radio button and called function of click event.
Better if you use external css.
please write php related code as per your need.
I have a page where i am disabling the button after submit for 10 seconds and if validation succeeds form is submitted. However, the form is getting submitted even though validation is returning false.
I tried using
<input type="submit">
instead of image still same issue.
Below is the code:
<html>
<head>
<script>
function enableMe(myBtn) {
myBtn.disabled = false;
}
function doValidation() {
document.getElementById('hidden1').value = 'true';
return false;
}
</script>
</head>
<body>
<form id="loginForm" method="post" action="https://www.mySite.com/authService">
<label for="userid">Username</label><br/>
<input type="text" value="" id="userid" name="userid"/>
<br/>
<label for="password">Password</label>
<br/>
<input type="password" value="" id="password" name="password"/>
<br/>
<br/>
<input type="image" id="submitBtn" src="btn_login.gif"
onsubmit="this.disabled=true; setTimeout(enableMe,10000,this); return doValidation();">
<input type="hidden" id="hidden1" name="hidden1" value="false"/>
</form>
</body>
</html>
Validation should be attached to form, not input element...
<form id="whatever" ... onsubmit="return doValidation();">