Forms action - Error 404 - not found - php

I am trying to send a email using forms and PHP. When I click on the send button I get t the error Error occurred: 404 - not found. submit.php file is in same dir as the webpage. Why can't it find the submit.php. My html code.
<form action="submit.php" id="form1" method="post">
<fieldset>
<label class="input">Date:<span><span><input type="text" id="datepicker" value=""/></span></span></label>
<label class="input">Enter Your Name:<span><span><input name="name"/></span></span></label>
<label class="input">Enter Your Email:<span><span><input name="email"/></span></span></label>
<label class="input">Subject:<span><span><input name="subject"/></span></span></label>
<label class="text">Enter Your Message:<span><span><textarea name="comment" cols="0" rows="0"></textarea></span></span></label>
<label class="input">Enter Code:<img src="php/captcha.php"><span><span><input type="text" name="vercode" /></span></span></label>
<label class="butt"><strong><b>clear</b></strong>
<strong><b>send</b></strong></label>
</fieldset>

When using <a>'s to use as form submit buttons, I would suggest using a JavaScript function and put it in the onclick of the anchor. Like this:
function doSubmit(form_name) {
// check if the object exists
if (document.getElementById(form_name)) {
// submit the form
document.getElementById(form_name).submit();
}
// prevent the link from clicking
return false;
}
<strong><b>send</b></strong>
The same can be done for the reset button.
In this way, you never navigate your browser to a "javascript:...etc.." location (that does not exist). I'm not sure if this is the case here, but just as a best practice advise then. :)

Related

How to do form validation using HTML and jQuery without redirecting to another page?

I have written a HTML form and script to validate the form. The issue is that whenever I go to submit the form I get redirected to a page that tells me "Your file was not found".
Is there a specific way I have to call the script? or any way I can force it to run without redirecting me?
<form id="bioform" action="/action_page.php">
<label>Biography</label><br>
<textarea placeholder="Enter bio here" name="bio" rows="10" cols="50"></textarea><br><br>
<label>Update Biography Picture</label><br><br>
<input type="file" name="bioimage"><br><br>
<button type="submit" onclick="function()">Update</button><br>
</form>
<script>
$(document).ready(function () {
$('#bioform').validate({
rules: {
bio {
required: true,
minlength: 5,
},
}
messages: {
bio {
required: 'Please enter a biography.',
minlength: 'Please enter a valid biography.',
},
}
});
});
</script>
Add enctype="multipart/form-data" to form tag
<form id="bioform" action="/action_page.php" enctype="multipart/form-data">
<label>Biography</label><br>
<textarea placeholder="Enter bio here" name="bio" rows="10" cols="50">
</textarea><br><br>
<label>Update Biography Picture</label><br><br>
<input type="file" name="bioimage"><br><br>
<button type="submit" onclick="function()">Update</button><br>
</form>
Is the forward slash when specifying where it sends to necessary? For instance:
<form id="bioform" action="/action_page.php">
Should be replaced with:
<form id="bioform" action="action_page.php">
Since you're using jQuery, try using this:
e.preventDefault()
The e.preventDefault() method stops the default action of an
element from happening. For example: Prevent a submit button from
submitting a form. Prevent a link from following the URL.
Also, you need to add method attribute inside the <form> tag with value POST and also the enctype="multipart/form-data" since you're dealing with file upload.
Go to this fiddle and check if it works for you: https://jsfiddle.net/z0db4vq0/1/

codeigniter - form returning no data

I'm trying to create a page with a form using ci.
When i submit the form, the controller says that I have no data that's been submitted.
I can't see where my error lies.
Here's the view:
<?php echo validation_errors(); ?>
<?php echo form_open('widgets/search/'.$hardwaremodel.'/'.$objectid.'/'.$name.'/'.$fd); ?>
<div class="form-group">
<label for="search">Last 4 characters of address:</label>
<input type="text" class="form-control" id="searchstring" placeholder="last 4 characters" size="4">
</div>
<button type="submit" class="btn btn-default">Search</button>
<button type="cancel" class="btn btn-default">Cancel</button>
</form>
Once the page renders, the form tag ends up looking like this:
<form action="http://myserver/myciapp/index.php/widgets/search/205406zl/5461/SW-1/SW1net" method="post" accept-charset="utf-8">
The controller:
public function search()
{
$searchstring = $this->input->post(); // form data
var_dump($searchstring);
exit;
}
The results of the var_dump shows:
bool(false)
Thanks
EDIT 1
I haven't posted the entire HTML page that includes the form... but I display some of the fields passed in the URI as headings on the page - just before I create the form. Hope that clarifies...
Would this impact the POST data? Why is that relevant?
Thanks
A few things I'd suggest doing. First is, if you are going to include other variables in the form_open tag, I would add those variables to your controller, and put them in the form_open tag as URI strings. This will allow the form validation to work if you are going to echo out validation errors.
Also, you should be calling a name on the input->post() to get the specific item, (but you don't need to to get all POST data).
Controller:
public function search($hardwaremodel, $objectid, $name, $fd) {
$searchstring = $this->input->post('search_string'); // form data
var_dump($searchstring);
exit;
}
View:
<?php echo form_open('widgets/search/'.$this->uri->segment(3).'/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6)); ?>
<div class="form-group">
<label for="search">Last 4 characters of address:</label>
<input type="text" class="form-control" id="search string" name="search_string" placeholder="last 4 characters" size="4">
</div>
Form elements are referenced by name attribute which is missiong on input field searchstring.
Add:
name="searchstring"
on your input field.
in this code you have not used name attribute.You use id.
try this one
<input type="text" name="searchstring" value="xyz">

Attempting to save user input via HTML form and then refer back to it later

I am currently in the process of designing a Computer Aided Learning package. The welcome screen simply requires the user to type in their name into a text field and click a button which directs them to 'subject selection' page. How would I go about storing the user input and how, if possible, could I refer to the stored value on a seperate html page?
<div id="inputbox">
<form>
<input type="text" name="field" class="textInput" />
</form>
</div>
<div id="introsubmit">
</div>
HTML:
<div id="inputbox">
<form action="script.php">
<input type="text" name="field" class="textInput" />
</form>
</div>
<div id="introsubmit">
</div>
That will send the form to script.php (if no 'action' defined, it will send the form to the very same page, if that html is inside script.php then it will work the same, and if no "method" is defined, if will use GET, you can set <form action="script.php" method="POST"> to change this):
script.php:
<?php
session_start();
$_SESSION['name'] = $_GET['name'];
?>
and then you reuse that variable in the same session, remember to always invoke session_start() before any output in your php.

Sending html form data as JSON to PHP using JQuery/AJAX

When i click my login button, it just reloads the page for some reason. it should alert the string i echo from my php page.
This is my login.js code:
$(document).ready(function(){
$('#login').click(function(){
$('#msgLoginStatus').show();
$('#msgLoginStatus').html("processing...");
$.post('login.php',{username:"bob",password:"pass"}, function(data){
alert(data);
});
});
});
my login.php:
<?php
echo "message";
?>
and my form:
<form id="loginForm" action="" method="post">
<fieldset id="body">
<fieldset>
<label for="username">Username</label>
<input type="text" name="username" id="username" />
</fieldset>
<fieldset>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</fieldset>
<button id="login">login</button>
<label for="checkbox"><input type="checkbox" id="checkbox" />Remember me</label>
<br />
<p id="msgLoginStatus" style="display:none"></p>
</fieldset>
<span>Forgot your password?</span>
</form>
There are no errors in browser console. I tried this also using $.ajax, it returned an error, i tried putting the error variable in an alert, but when it alerted, it was an empty string. Anyone have an idea whats wrong?
Your login button has an ambiguous action - add type="submit" like this:
<button id="login" type="submit">Login</button>
Now if you really want to execute an explicit POST with JavaScript, call e.preventDefault so the browser's automatic "submit" action will be suppressed.
e.preventDefault();
$.post(...);
But it will probably be better to let the form submit itself. To do this specify the correct action="login.php" attribute in the form:
<form id="loginForm" action="/login.php" method="post">
Keep your existing "click" handler on the login button, just remove the "$.post" part and let the browser handle the posting. You'll still get the nice "processing..." text.
Even better, handle the "submit" event on the form instead of the "click" event on the button:
$('#loginForm').submit(function(e) {
$('#msgLoginStatus').show();
$('#msgLoginStatus').html("processing...");
});
This way you'll get the nice updates whether the user submits the form using the button or by pressing "enter" on the keyboard.
Try:
$('#login').click(function(e){
e.preventDefault();
$('#msgLoginStatus').show();
$('#msgLoginStatus').html("processing...");
$.post('login.php',{username:"bob",password:"pass"}, function(data){
alert(data);
});
});
That prevents a "normal" submit from happening (which, I take, is why you are not getting any errors).
See http://api.jquery.com/event.preventDefault/
Add e.preventDefault(); to the clck handler (and grab the event object in the handler as e).
Or you can Just set the button type = 'Button' and not Submit. THis will also run your code
<button id="login" type="button">Login</button>
In this way you don't have to halt the browser's event

how to submit a from with validation in PHP?

I want to submit this form through PHP. with validation for required field and validation for phone number and email field also
<form action="" method="" id="get-protected">
<div class="row requiredRow">
<label for="txt_FirstName">
First Name</label>
<input id="txt_FirstName" type="text" class="required" title="First Name. This is a required field" />
</div>
<div class="row">
<label for="txt_LastName">
Last Name</label>
<input id="txt_LastName" type="text" title="First Name. This is a required field" />
</div>
<div class="row">
<label for="txt_Phone">
Phone</label>
<input id="txt_Phone" type="text" title="First Name. This is a required field" />
</div>
<div class="row requiredRow">
<label for="txt_Email">
Email</label>
<input id="txt_Email" type="text" class="required" title="Email. This is a required field" />
</div>
<div class="row">
<input type="submit" value="" class="button" />
</div>
</form>
In your method attribute inside your form, you need to declare either post or get.
Since your action attribute is "" it will submit to the page itself rather than redirecting to another page, so you can have your code that checks for validation in the same PHP file. First validation that is often checked is if the variable has a value by using isset:
if(isset($_POST['txt_Phone'])) { ... }
This just checks that the Phone number field does not contain empty data. I strongly suggest you perform other validation checks on the POST array so you do not have any users posting malicious code.
You can use functions like htmlspecialchars to prevent user-supplied text depending on what you plan to do with the values
Here are some references to help you along the way in the order they should be viewed.
Form Validation using PHP - PHP and MySQL Tutorial
PHP Advance Form Validation Tutorial
PHP Tutorial Part 2: Form Validation
Your form tag needs a target in the action field and a method in the method field (either GET or POST). So make the action your PHP script.
<form name="input" action="form_submit.php" method="get">
As for field validation, you will either have to parse that inside of the PHP and return a response or use Javascript in the browser to check on the fly.
Here is the shcema of such a script:
if ($_SERVER['REQUEST_METHOD']=='POST') {
//data validation:
$err="";
if (valid_phone($_POST['phone'])) $err="Wrong phone no";
if (!$err) {
//record data:
$sql="...";
query($sql);
Header("Location: ".$_SERVER['REQUEST_URI']); //redirect and exit
exit;
}
}
?>
<html>
<head></head>
<body>
<? if ($err) ?> <font color=red><b><?=$err?></b></font>
<form method="POST" id="get-protected">
here goes your form
Okay, firstly, I like to set the form action to <?=$_SERVER['REQUEST_URI']?> to submit it back to the current page, but leaving it as you have it will work fine too.
Secondly, you need to give all your <input>s a name attribute. This is the variable name that PHP will see.
When your users get an error (something doesn't validate correctly) you don't want all the data they entered to disappear. That means you have to set the value attributes of each input to what they had previously entered. Thus, your form starts to look like this:
<form action="<?=$_SERVER['REQUEST_URI']?>" method="" id="get-protected">
<div class="row requiredRow">
<label for="txt_FirstName">
First Name</label>
<input id="txt_FirstName" type="text" class="required" title="First Name. This is a required field" name="first_name" value="<?=htmlspecialchars($_POST['first_name'])?>" />
</div>
...
<div class="row">
<input type="submit" name="submit" value="" class="button" />
</div>
</form>
If you didn't know <?= is a basically a shortcut for <?php echo but it will only work if your server has short tags enabled. Some people prefer to type it out the long way (in case they want to switch servers later, or for future-compatibility, or because they're nutbars), but I say screw them, I'm lazy.
This page that has the form on it, has to saved with a .php extension (well, technically it doesn't have to, but that's another story). Then you need to handle you form validation. You have to code that up yourself. It might look something like this (put it above your form somewhere)
<?php
if($_POST['submit']) {
$errors = array()
if(empty($_POST['first_name'])) $errors[] = 'please enter your first name';
if(empty($errors)) {
// save the data to database or do whatever you want with it
header('redirect:succcess.php');
} else {
foreach($errors as $e) {
echo $e;
}
}
}
?>
It's been a while since I've coded in PHP so forgive me if there are syntax errors. That's the jist of it anyway, I'm sure you can find validation libraries out there if you Google. Might take some of the grunt work out of trying to validate email addresses and such.
Using Javascript you can do the validation for this form.For each condition you can use return true and return false,based on the condition.Then you can submit the value.
Using action attribute in form tag the values will be submitted to that file.

Categories