Form submission with php after js validation - javascript

I want to submit my form to a PHP file called regier.php after validating it using JavaScript. The HTML form is given below:
HTML FORM:
<form onsubmit="return validateForm()" action="register.php" method="post" class="sign-up-form">
<div class="input-field">
<input type="text" placeholder="Nome" name="username" id="username" onkeyup="validateName()"
<span id="name-error"></span>
</div>
<div class="input-field">
<input name="password" type="password" placeholder="Password" id="password" onkeyup="validatePassw()" />
<span id="passw-error"></span>
</div>
<input onclick="return validateForm()" type="submit" class="btn" value="Registrati">
JAVASCRIPT VALIDATION:
The following is the JavaScript validation code:
function validateForm() {
if (!validateName()) {
validateName()
}
if (!validateEmail()) {
validateEmail()
}
if (!validatePassw()) {
validatePassw()
}
}```

Set the action attribute to a file/code to validate the form:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Related

submit form on keyup on elements added dynamically via ajax

I have 2 forms with the same ids.
I want when you type a key in the text area with id=ingevuldNotitie then javascript will submit the form. Is there a way to do this and so how?
<div id="response">
<div id="child_response">
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan();">
<input type="text" id="clientNotitieDatum" value="2022-10-17" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan();">
<input type="text" id="clientNotitieDatum" value="2022-10-18" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
</div>
</div>
<script>
// I tried this
$("#response").bind("keyup", function () {
$(this).closest('[name=clientNotitieDatum]').submit();
});
// And I tried this
$("text[name='ingevuldNotitie']").bind("keyup", function () {
$(this).closest('form').submit();
});
</script>
Because the elements are dynamically added via ajax, you should use $(document).on('keyup', "textarea[name='ingevuldNotitie']", function() {.
Alo, in your forms onsubmit, add an argument to the function:
onsubmit="return notitieOpslaan(this);"
And use that argument to get the form's data, such as clientNotitieDatum.
See working demo below:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="response">
<div id="child_response">
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan(this);">
<input type="text" id="clientNotitieDatum" value="2022-10-17" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan(this);">
<input type="text" id="clientNotitieDatum" value="2022-10-18" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
</div>
</div>
<script>
$(document).on('keyup', "textarea[name='ingevuldNotitie']", function() {
$(this).closest('form').submit();
});
function notitieOpslaan(theForm) {
var clientNotitieDatum = theForm['clientNotitieDatum'].value;
console.log('form would have been submitted', clientNotitieDatum);
return false;
}
</script>

my textarea text is clearing after button submits it

my code below deletes the text from my textarea that is inserted by a user after clicking the submit button but what i want is i want the text to be in my textarea after submit.
<form action="/search" name="myform" id="formid" method="post" >
<div class="input-group" >
<!--
<textarea name="input" id="textArea" />
<button type="submit" onclick="return fill()" >Search</button>
</button>
</div>
</form>
<script type="text/javascript">
function fill()
{
var x = document.getElementById('textArea').value;
document.getElementById('demo').innerHTML=x;
}
</script>
Simply try to add onSubmit="return false;" at the form tag
<form action="/search" onSubmit="return false;" name="myform" id="formid" method="post" >

How to pass data from one form to another form on a different page in laravel 5.7?

I want to pass an email value in a textbox from welcome page to register page in Laravel without using the database. I tried the following code in simple PHP page it works fine but when I use in Laravel 5.7 page it shows an error.
Welcome page
<form method="POST" action="register">
<input type="text" size="40" name="email">
<input type="submit" name="submit">
</form>
Register Page
<form method="POST" action="register">
<input type="email" size="40" name="reg_email" value="<?php echo $_POST['email']; ?>">
<input type="submit" name="submit">
</form>
I want that when I write an email in welcome page form textbox & submit, it shows or display in a register page form email textbox without using Database.
You could send the email as a query string parameter to the registration page.
<!-- Welcome Page (Note the GET method) -->
<form method="GET" action="/register">
<input type="text" size="40" name="email">
<input type="submit" name="submit">
</form>
Make sure you're including the csrf token in your request.
<!-- Registration Page -->
<form method="POST" action="/register">
#csrf
<input type="email" size="40" name="reg_email" value="{{ request('email') }}">
<input type="submit" name="submit">
</form>
try this:
'''' Welcome page: where user would enter the email before proceeding to registration page
<form method="POST" action="{{ route('welcome') }}">
{{ csrf_field() }}
<input type="text" size="40" name="email">
<input type="submit" name="submit">
</form>
'''' Register Page: this is where the email displays inside the input name reg_email
<form method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<input type="email" size="40" name="reg_email" value="{{ $myemail }}">
<input type="submit" name="submit">
</form>
//the controller collects the email input from the welcome page
public function Welcome(Request $request)
{
$email = $request->input('email');
$data['myemail']=$email; //assign the email variable myemail data to be pass to registration page view
return view('registerpage',$data); //pass the data to the view
}
//Route
Route('/welcome-page','MyController#Welcome')->name('welcome'); //ofcourse the route using name route welcome

Hide form and show div

So I have this
<?php
if(isset($_POST['submit'])) {
$error = "test";
}
?>
<div id="first" class="1">
<form action="" method="post" id="myform">
<p>
<label for="textfield">Text Field:</label>
<input type="text" name="name" id="name">
<br>
<input type="submit" name="submit" formmethod="POST">
</p>
</form>
</div>
<div id="second" class="2" style="display:none">
<?php echo $error; ?>
</div>
<script>
$(document).ready(function() {
$("#myform").submit(function(e) {
e.preventDefault();
$("#first").hide();
$("#second").show();
});
});
</script>
So, everything is OK with javascript, the form is hiding, div is displaying, but the php isnt working. The form is submitting only to js and not to php.
You need an action, it's not doing anything because it's TOLD to not do anything
<form action="name_of_this_file.php" method="post" id="myform">
or
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" id="myform">
You have no php action handler listed.

Use JavaScript to sign into a form

I'm new to JavaScript and I want to log in to a webpage. This code will take me to the web page but won't sign in. I know it knows the values of username/password because if I do alert(loginForm.elements["username"].value) it displays "someUsername".
I'm not sure how to get these values to be entered into the boxes and hit submit. Any advice helps, thanks!
<script>
function login() {
document.loginForm.action = "http://websiteToLoginTo.com";
document.loginForm.submit();
}
</script>
<body onLoad="login()">
<form name="loginForm" method="post">
<input type="hidden" name="username" value="someUsername">
<input type="hidden" name="password" value="somePassword">
</form>
</body>
The website I'm trying to connect to has this:
<form action="/Authn/UserPassword" method="post" name="loginForm">
<input id="username" name="t_username">
<input id="password" name="t_password">
<input type="submit" name="login" value="Login" class="submit">
</form>

Categories