PHP - My form page freezes after submitting form - php

Here's the thing. When I submit the form, it works every time UNLESS my Title field is blank.
My HTML on page:
Title: <input type="text" name="title"><br />
Description:<br />
<textarea name="desc"></textarea><br /><br />
Location: <input type="text" name="where"><br />
<input type="submit" value="Submit">
My PHP having to do with the title input, on the other page:
if(empty($_POST["title"]) or !ctype_alpha(str_ireplace(" ","",$_POST["title"]))){
$ge=True;
$et=True;
}
The above if statement just checks to see if the title entered contains letters and spaces ONLY, and to make sure it isn't blank.
It's only when that one text field is blank, and nothing else.
I'm new to PHP so any help would be awesome.
Thanks for your time!
- Eric
EDIT
Sorry, should have been more descriptive. The page acts as if it's trying to submit the form (the spinning loading icon thing is there), but it never makes it to the process page. The form page just becomes unresponsive.

You can try this,
<html>
<body>
<form method="POST">
Title: <input type="text" name="title"><br />
Description:<br />
<textarea name="desc"></textarea><br /><br />
Location: <input type="text" name="where"><br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
if(empty($_POST["title"]) or !ctype_alpha(str_ireplace(" ","",$_POST["title"])))
{
$ge=True;
$et=True;
echo 'empty title';
exit();
}
echo 'form submitted';
}
?>

Related

switch function to change page based on user input not working

I am trying to change page based on user input in form. The user enters their tag and it changes to that users page with details. Currently, it just keeps attempting to change page then eventually times out. Here is my code:
SWITCH STATEMENT
if(isset($_POST['submit'])) {
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
...//
default:
header("Location: index.php");
}
}
FORM
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
So if user enters player 1 in form then submits, it should change to commander.php page, but it isn't.
Could anyone point me in correct direction thanks.
First of all, change first line to
if(isset($_GET['submit'])){
2nd give a 'name' attribute to submit button
<input type="submit" class="submit" value="SUBMIT" name="submit"/>
I just looked at your code and realized your form was sending data using the HTTP GET method but your PHP script was checking for HTTP POST method in the if(isset($_POST['submit'])). So I modified the PHP as shown below.
if(isset($_GET['clan_tag'])){
$name = $_GET['clan_tag'];
switch($name) {
case "player1":
header("Location: commander.php");
break;
case "player2":
header("Location: officer.php");
break;
default:
header("Location: index.php");
}
}
?>
I also modified the HTML as shown below.
<form action="" method="get">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
Try in this way:
In the FORM:
attribute method write "post" in action= write "path where is your php file"
i.e.
<form action="path/yourfile.php" method="post">
<input name="clan_tag" type="text" class="box" placeholder="Enter the clan players tag" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>
in PHP file
if(isset($_POST['clan_tag'])) {
$name = $_POST['clan_tag'];
}
else{
$name="";//your code
}
....

How to set two buttons on one form html&php

This form should calculate numbers and save
Now there are two buttons One is call Calculator and two call Save
If I press Calculator
I get the form action is going to file name save.php And I do not want it that way
How can I set it up that button do something else
Example
Calculator = Calculator
Save = save.php
Is it possible to set it
Because it is one form
Thanks to anyone who can help
<?php
error_reporting (0);
$NUM = $_POST["NUM"];
$NUM2 = $_POST["NUM2"];
$NUM = "$NUM";
$NUM2 = "$NUM2";
$subtotal= $NUM+$NUM2;
?>
<form action="save.php" method="POST" name="Calculator">
<p>
<input name="NUM" type="text" value="<?php echo $_POST["NUM"]; ?>" />
</p>
<p>+</p>
<p>
<input name="NUM2" type="text" value="<?php echo $_POST["NUM2"]; ?>" />
</p>
<p>
<input name="subtotal" type="text" value="<?php echo "$subtotal";?>" />
</p>
<p>
<input name="submit" type="submit" value="Calculator" />
<p>
<input name="submit" type="submit" value="Save" />
</p>
</form>
You can have all the logic in a single PHP script (no need to direct to a different script depending on the button). If the logic is complicated, use include statements in order to separate the code.
Name the buttons differently:
<input name="calculator_submit" type="submit" value="Calculator" />
<input name="save_submit" type="submit" value="Save" />
Then in PHP:
if (isset($_GET['calculator_submit'])) {
// ...
} else if (isset($_GET['save_submit'])) {
// ...
} else {
// ...
}
If you really need different PHP script, then you'll have to go with Javascript (function will change the form action when a submit is clicked).
Since you are now using two submit buttons, both will submit the form and go to save.php.
Make your "calculator" button an input type=button instead of submit, and handle it via JavaScript.
Just FYI:
HTML5 allows to define a different form target URL by specifying the formaction attribut on a submit button – but browser support is lousy as of now.
Form and Buttons
<input name="submit" type="button" onclick="submitForm('Calculator')" value="Calculator" />
<input name="submit" type="button" onclick="submitForm('Save.php')" value="Save" />
Some jquery:
function submitForm(path) {
$('#Calculator').attr('action', path);
$('#Calculator').submit();
}

PHP to display text input content

Im wanting to display the content that a user types into a text box lower down on the page once they've clicked a submit button.
I'm new to PHP and so far i've got this:
<?php if(isset($_POST['submit'])) {echo 'You entered: ', ($_POST['name']);}?>
Any ideas as to what i'm doing wrong?
Try this:
<form action="" method="post">
<input type="text" name="name" />
<input type="submit" name="submit" />
</form>
<?php if(isset($_POST['submit'])) {echo 'You entered: '.$_POST['name'];}?>

HTML submit forms using PHP

I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];

Form submitting but I don't know how or why?

I've made a normal HTML form which when submitted, sends information to another page and that page processes the data and adds it to the database. But for some reason, if I refresh the 1st page which contains the form, an entry is being made into the database even though I can't see any reason for it to do so.
This is the code for the form:
<?php $owner = $_GET['user']; ?>
<h2 class="dotted"><?php _e('Add feedback for user','appthemes'); echo " - ".$owner;?></h2>
<form id='feedback' action='http://www.example.co.uk/add-feedback-response/?user=<?php echo $owner; ?>' method='POST' accept-charset='UTF-8'>
<fieldset >
<b>Your Username:</b> <?php echo $current_user->user_login; ?><br/><br/>
<b>Rating: </b>
<input type="radio" name="rating" value="positive" /> Positive
<input type="radio" name="rating" value="neutral" /> Neutral
<input type="radio" name="rating" value="negative" /> Negative<br/><br/>
<label for='item' ><b>Item bought:</b></label>
<input type="text" name='item' id='item' maxlength="500"/><br/><br/>
<label for='comment' ><b>Comment:</b></label>
<textarea name='comment' id='comment' maxlength="2500"></textarea><br/><br/>
<input type="submit" id="submit-button" value="Submit Feedback" />
</fieldset>
</form>
Is there something I'm missing? Thanks for any help
It is probably because you're trying to submit form again. Is browser alerting you that you're sending data again?
to restrict this, use
header("Location: ".$_SERVER['HTTP_REFERER']);
in proccessing form
If you refresh the form after it has submitted once, the form will be submitted again. (the browser would have warned you, "Are you sure you want to submit the page again" - didn't it?) Refresh is actually "repeat the last request".
If you think such a scenario is possible in your webapp, you will have to handle duplicate requests.

Categories