Pause form from submitting with javascript - php

I am trying to do a submit form with photos. After the user loads the photos he presses the submit button, I want the form to pause for 10 seconds, animate a progress bar for those 10 seconds and then submit the form, can you guys say what I did wrong, it doesn't seem to submit the form after 10 seconds.
Here is the code:
HTML:
<form action="uploadpic.php" method="post" id="upload_form">
<input type="text" name="title" id="title">
<p id="title_p">Title</p>
<hr />
<input type="text" name="theme" id="picture_theme" size="40"/>
<p id="theme">Picture Theme<img src="../simages/info.gif" id="info" width="12" height="12" style="margin-left:10px;"></p>
<hr />
<div class="custom-upload">
<input type="file" name="picture" id="true_pic" />
<div class="fake-file">
<input disabled="disabled" >
</div>
</div>
<p id="upload_pic">Upload picture</p>​
<input type="submit" name="submit" id="submit" value="Upload" />
</form>
JAVASCRIPT:
form = document.getElementById("upload_form");
size=1;
form.onsubmit = function()
{
if (size < 10)
{
setTimeout(delayedSubmit,1000);
}
return false;
}
function delayedSubmit() {
size++;
if (size<5)
{
setTimeout(delayedSubmit,1000);
alert("Counting "+size);
}
else
{
alert("Form submitted");
form.submit();
}
}
PHP :
<?php
if ($_POST['submit'])
{
$title = $_POST['title'];
$theme = $_POST['picture_theme'];
echo $title," ",$theme;
}
?>
I can tell that the form won't submit anything by the fact that the php variables won't show anything, and then page doesn't load.

When a form has a button with the name and/or id "submit", it won't work anymore (my old post was wrong).
So what you need to do is to change the name/id of the button:
<input type="submit" name="submit-button" id="submit-button" value="Upload" />
Remember: You need to change your PHP, too:
if ($_POST['submit'])
{
$title = $_POST['title'];
$theme = $_POST['picture_theme'];
echo $title," ",$theme;
}
to
if ($_POST['submit-button'])
{
$title = $_POST['title'];
$theme = $_POST['picture_theme'];
echo $title," ",$theme;
}

I'd simplify the javascript:
form = document.getElementById("upload_form");
size=0;
form.onsubmit = delayedSubmit;
function delayedSubmit () {
if (++size < 5) {
alert("Counting "+size);
setTimeout(delayedSubmit,1000);
return false;
}
alert("Form submitted");
form.submit();
}
And - of course - remove (or change) id and name from the submit button, e.g:
<input type="submit" value="Upload" />
e.g.: http://jsbin.com/equyit/1/edit

Related

How do i clear this array by user input in php/html?

I am working on a program that has the user type in their course, first name, last name, and description of a program. The code is mostly done except for getting the clear the array button to work. When I use the unset array to clear the array on its own, it works but then the user cant enter in more data. I want to have the user be able to clear the data. Here is my code:
<?php
session_start();
?>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q="+str, true);
xmlhttp.send();
}
}
</script>
<?php
function clear(){ //this is the problem
unset($_SESSION['courses']);
return true;
}
?>
</head>
<body>
<form method="POST">
Course: <input type="text" name="courses" />
<br /><br />
First Name: <input type="text" name="firstname" />
<br /><br />
Last Name: <input type="text" name="lastname" />
<br /><br />
Description: <input type="text" name="description" />
<br /><br />
<input type="submit" name="submit" value="Submit">
</form>
<?php
// First we check if the form has been sent and we have a value
if (!empty($_POST['courses'])) {
if (!isset($_SESSION['courses']))
$_SESSION['courses'] = array(); // Initialize the array if it doesn't exist
// Add the value to our array
$_SESSION['courses'][] = array("course" => $_POST['courses'],
"firstname" => $_POST['firstname'],
"lastname" => $_POST['lastname'],
"description" => $_POST['description']);
}
// If there are values to show, print them!
if (!empty($_SESSION['courses'])) {
foreach ($_SESSION['courses'] as $course) {
echo $course['course']." ".
$course['firstname']." ".
$course['lastname']." ".
$course['description']." ".
"<br />";
}
}
?>
<input type="submit" name="Clear" value="Clear" onclick="clear()"> //this is the problem
<?php
?>
</body>
</html>
Can someone please help?
<?php
// there is nothing wrong with this function.
function clear() {
unset($_SESSION['courses']);
return true;
}
?>
Okay, this function is fine, there is nothing wrong with it. But, you can't use this function like:
<input type="submit" name="Clear" onclick="Clear()" /> <!-- this is the problem -->
You see that onclick="Clear()", and that php function clear()? Yeah, you can't execute php functions with a html onclick="". You can only do that with javascript functions.
But you can do something like this:
<?php
if(isset($_POST['Clear']))
{
// if the user submits the form, then the following code will be executed.
clear();
}
?>
<input type="submit" name="Clear" value="Clear" onclick="clear()">
clear() would be calling a javascript function. You have correctly written a php function.
Check the value of the submit button "Clear" to be "clear" and if true run the PHP function clear().
if ($_POST['Clear'] === 'clear') {
clear();
}

How to remove button after submit form using PHP

I need one help.I need to totally remove the submit button after submit the form and when it will be submitted the button will display to user.I am explaining my code below.
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px"> Name :</span>
<input type="text" name="u_name" id="name" class="form-control" placeholder="Add Name" onKeyPress="clearField('name');">
</div>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>
</form>
complain.php:
require_once("./include/dbconfig.php");
if(isset($_REQUEST['complainSubmit']))
{
// ......data is collecting here....
}
when data are submitted successfully the below part is executing.
<script type="text/javascript">
var phpVar = "<?php echo $_GET['success'];?>";
//console.log('php',phpVar=='');
if(phpVar == 1 && phpVar!=''){
alert('Submitted successfully.');
//var subButton=document.getElementById('addProfileData');
//subButton.disabled=false;
}
else if(phpVar == 0 && phpVar!=''){
alert('Unable to add.\\nTry again.');
}
else{
// nothing
}
</script>
<script>
function checkForm(){
var s=document.billdata;
if(s.u_name.value==''){
alert('Please enter name');
s.u_name.focus();
s.u_name.style.borderColor = "red";
return false;
}
}
</script>
Here i need to button hide when the data is going to submit and it will again display after the submit.Please help me.
A javascript code such as:
document.getElementById("your_div").innerHTML = "";
will erase the content of your div.
So, byt tagging the entire form, then erasing its contents, you can "hide" it.
Why you dont add a hide CSS-Selector or remove it by checking via if-statement?
CSS-Version:
<input type="submit" class="btn btn-success <?php ($isSubmitted ? ' hideme' : '')?>" name="complainSubmit" id="addProfileData" value="Submit">
Except a new CSS-Selector: .hideme { display:none }
Via PHP/Template:
html...
<?php if(!$isSubmitted) : ?>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit">
<?php endif; ?>
html...
Dont forget: You dont need close input html-tag: <input/> just <input>
And what is wrong to hide the button via JS?
document.getElementById("addProfileData").style.display = "none";
UPDATE:
Prompt hiding after clicking:
<button onclick="javascript:this.style.display='none'">
Submit Button
</button>
in your example:
<input onclick="javascript:this.style.display='none'" type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>

PHP: 2 Forms, 1 Being File Upload

I have a single profile page that I want to upload a photo on as a separate action. I have the first form submitting to the page successfully, it is when I submit the photo form that the page returns blank with an empty message.
HTML
<form method="post" action="profile.php" id="main">
<input name="txtFirstName" type="text" value="<?php echo $sFirstName; ?>">
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<form method="post" action="profile.php" id="photo_upload" enctype="multipart/form-data">
<img src="<?php echo($sPath); ?>" height='100' width='100' id="imgProfile" />
<br>
<input type="file" name="fUpload" id="fUpload">
<br>
<input type="submit" name="btnUploadPhoto" value="Upload" class="cancel"/>
</form>
PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['btnSubmit']) {
$sFirstName = $_POST['txtFirstName'];
}
else if ($_POST['btnUploadPhoto']) {
// DO MY MOVE LOGIC
}
}
When the "btnSubmit" is called the page loads and the textbox gets the name that was entered. When the "btnUploadPhoto" is called I get this on the screen and nothing else:
{error: '', msg: '' }
How do I get the page to reload with the original form?
As you are uploading a file, you will want to check if a file has been submitted in your PHP code. Here is the code to do this:
isset($_FILES['fUpload'])

php javascript alertbox have to click 2 times before it show

I'm doing php that is textbox a value empty it will open a alertbox (I'm using javascript in here )
this is my code
<?php
include('config.php');
if(isset($_POST['submit'])){
$username=$_POST['username'];
?>
<script>
function validate(){
if(document.forms[0].username.value==""){
window.alert("You must enter both values");
return false;
}
}
</script>
<?php
}
?>
<html>
<div><p>Member Profile</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username<br>
<input class="user" type="text" name="username" id="username" /><br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</html>
The problem is i have to click 2 times before the alert show
please help me to solve this problem
It's because the script is inside the php if(isset){} block, one click submits the form, which generates the script and then it works the second time.. try this setup instead:
<?php
include ('config.php');
if (isset($_POST['submit']))
{
$username = $_POST['username'];
}
?>
<html>
<head>
<script>
function validate () {
if (document.forms[0].username.value == "") {
window.alert("You must enter both values");
return false;
}
}
</script>
</head>
<body>
<div>
<p>
Member Profile
</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username
<br>
<input class="user" type="text" name="username" id="username" />
<br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</body>
</html>
Edit:
I've moved the script tag inside the head tag. I'm not sure if there are any implications for having the script outside but just to be sure I've moved it.
2nd Edit: (OCD is kicking in)
I've added body tags, not sure if you copied and pasted this code but it looked weird to me :)

HTML Submit Button Branching

On a series of web forms users needs to be able to move forwards and backwards. However I want to process the form irrespective of which direction they are going.
The buttonshave name of "submit" and values of "back" and next". How do I interpret the value?
This doesn't work :-)
if(isset($_POST[$submit['back']])) { header('location: ../pages/create_page1.php'); }
if(isset($_POST[$submit['next']])) { header('location: ../pages/create_page3.php'); }
The the name of the field is called submit, so you need to check if the field has been submitted and what its value is:
if ( isset($_POST['submit]') && $_POST[submit] == 'back' )
However you aren't using submit buttons, you are using server side image maps, so the value might not have been submitted (depending on the browser).
Instead you need to give the fields different names, and then check to see if co-ordinates were sent for those fields.
<input type="image" name="submit_back" src="../images/arrows/back_50.png" width="121" height="50" border="0" alt="Back" />
if ( isset($_POST['submit_back_x']) )
Try to use jquery:
$('#some_button').click(funnction(){
var $Data = $('#some-data-input').val();
if($Data.length > 0){ // or some else
window.location.href = "../pages/create_page3.php";
} else {
// other action
}
});
Try this javascript function...
<script language=javascript>
function redirect()
{
if(document.value=="Next")
{
document.form.action="next.php";
}
else if(document.value=="Back")
{
document.form.action="back.php";
}
return true;
}
</script>
<form method="post" name=form onsubmit='return redirect();'>
<p>Click...</p>
<input type="submit" name="back" value="Back"/>
<input type="submit" name="next" value="Next"/>
</form>
You can't get the button value with $submit, it should be $_POST['submit'].
You can try this script.
<?php
if(isset($_POST['next'])) {
$submit = 'Next button was clicked.';
// header(...);
} elseif(isset($_POST['back'])) {
$submit = 'Back button was clicked.';
// header(...);
} else {
$submit = '';
}
?>
<form action="" method="post">
<p>Click...</p>
<input type="submit" name="back" value="Back"/>
<input type="submit" name="next" value="Next"/>
</form>
<br/><?=$submit;?>
Or with same button name and arrows like on your comments:
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'next') {
$submit = 'Next button was clicked.';
// header(...);
} elseif(isset($_POST['submit']) && $_POST['submit'] == 'back') {
$submit = 'Back button was clicked.';
// header(...);
} else {
$submit = '';
}
?>
<form action="" method="post">
<p>Click...</p>
<input type="image" name="submit" src="http://cdn4.iconfinder.com/data/icons/brightmix/128/monotone_arrow_left_small.png" value="back"/>
<input type="image" name="submit" src="http://cdn4.iconfinder.com/data/icons/brightmix/128/monotone_arrow_right.png" value="next"/>
</form>
<br/><?=$submit;?>

Categories