form with two buttons one saving form other submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

form with two buttons one saving form other submit

Post by jonnyfortis »

i have a form that now needs to be saved if it isn't complete, what the best way of doing this without making a whole new form.

i have included the whole form below.

Code: Select all

<?php
error_reporting(E_ALL);
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  //$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
  $insertSQL = sprintf("INSERT INTO plus_signup (terms, prop_id, StartDate, weeks, EndDate, Title, userid, password, sex, DOBDate, DOBMonth, DOBYear, Nationality, `Marital Status`, PhoneDay, PhoneEvening, PhoneMobile, email, Smoker, NextKin, CurrentAddress, PrevAddress, LivingStatus, Student, BankFinCCJ, BankFinArrears, BankFinBankR, BankFinNone, BankName, BankAccNum, BankSortCode, BankDuration, BankDCard, BankElectoral, LLName, LLTele, LLEmail, LLHost, LLAddress, GuName, GuTitle, GuDobDate, GuDobMonth, GuDobYear, GuSex, GuMarital, GuPhoneDay, GuPhoneEven, GuPhoneMob, GuPhoneEmail, GuCurrentAdd, GuPrevAdd, GuBankName, GuBankAccNu, GuBankSort, GuBankDurat, GuBankDebit, GuBankElect, GuEmpProffes, GuEmpAnnWa, GuEmpPayroll, GuSelfEmp, GuHaveAcc, GuAccName, GuAccContName, GuAccAdd, GuAccDayTel, GuAccMobTel, GuAccEmail, GuAccAdditional, GuEmployerNam, GuEmployerAdd, GuEmployerContactName, GuEmployerTele, GuEmployerEmail, GuEmployerCCj, GuEmployerArr, GuEmployerBankR, GuEmployerNone) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString(isset($_POST['terms']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['prop_id'], "text"),
		       GetSQLValueString($_POST['StartDate'], "text"),
		       GetSQLValueString($_POST['weeks'], "text"),
		       GetSQLValueString($_POST['EndDate'], "text"),
                       GetSQLValueString($_POST['Title'], "text"),
                       GetSQLValueString($_POST['userid'], "text"),
		       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['sex'], "text"),
                       GetSQLValueString($_POST['DOBDate'], "text"),
                       GetSQLValueString($_POST['DOBMonth'], "text"),
                       GetSQLValueString($_POST['DOBYear'], "text"),
                       GetSQLValueString($_POST['Nationality'], "text"),
                       GetSQLValueString($_POST['Marital_Status'], "text"),
                       GetSQLValueString($_POST['PhoneDay'], "text"),
                       GetSQLValueString($_POST['PhoneEvening'], "text"),
                       GetSQLValueString($_POST['PhoneMobile'], "text"),
		       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['Smoker'], "text"),
                       GetSQLValueString($_POST['NextKin'], "text"),
                       GetSQLValueString($_POST['CurrentAddress'], "text"),
                       GetSQLValueString($_POST['PrevAddress'], "text"),
                       GetSQLValueString($_POST['LivingStatus'], "text"),
                       GetSQLValueString($_POST['Student'], "text"),
                       GetSQLValueString(isset($_POST['BankFinCCJ']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString(isset($_POST['BankFinArrears']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString(isset($_POST['BankFinBankR']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString(isset($_POST['BankFinNone']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['BankName'], "text"),
                       GetSQLValueString($_POST['BankAccNum'], "text"),
                       GetSQLValueString($_POST['BankSortCode'], "text"),
                       GetSQLValueString($_POST['BankDuration'], "text"),
                       GetSQLValueString($_POST['BankDCard'], "text"),
                       GetSQLValueString($_POST['BankElectoral'], "text"),
                       GetSQLValueString($_POST['LLName'], "text"),
                       GetSQLValueString($_POST['LLTele'], "text"),
                       GetSQLValueString($_POST['LLEmail'], "text"),
                       GetSQLValueString($_POST['LLHost'], "text"),
                       GetSQLValueString($_POST['LLAddress'], "text"),
                       GetSQLValueString($_POST['GuName'], "text"),
                       GetSQLValueString($_POST['GuTitle'], "text"),
                       GetSQLValueString($_POST['GuDobDate'], "text"),
                       GetSQLValueString($_POST['GuDobMonth'], "text"),
                       GetSQLValueString($_POST['GuDobYear'], "text"),
                       GetSQLValueString($_POST['GuSex'], "text"),
                       GetSQLValueString($_POST['GuMarital'], "text"),
                       GetSQLValueString($_POST['GuPhoneDay'], "text"),
                       GetSQLValueString($_POST['GuPhoneEven'], "text"),
                       GetSQLValueString($_POST['GuPhoneMob'], "text"),
                       GetSQLValueString($_POST['GuPhoneEmail'], "text"),
                       GetSQLValueString($_POST['GuCurrentAdd'], "text"),
                       GetSQLValueString($_POST['GuPrevAdd'], "text"),
                       GetSQLValueString($_POST['GuBankName'], "text"),
                       GetSQLValueString($_POST['GuBankAccNu'], "text"),
                       GetSQLValueString($_POST['GuBankSort'], "text"),
                       GetSQLValueString($_POST['GuBankDurat'], "text"),
                       GetSQLValueString($_POST['GuBankDebit'], "text"),
                       GetSQLValueString($_POST['GuBankElect'], "text"),
                       GetSQLValueString($_POST['GuEmpProffes'], "text"),
                       GetSQLValueString($_POST['GuEmpAnnWa'], "text"),
                       GetSQLValueString($_POST['GuEmpPayroll'], "text"),
					   GetSQLValueString($_POST['GuSelfEmp'], "text"),
					   GetSQLValueString($_POST['GuHaveAcc'], "text"),
					   GetSQLValueString($_POST['GuAccName'], "text"),
					   GetSQLValueString($_POST['GuAccContName'], "text"),
					   GetSQLValueString($_POST['GuAccAdd'], "text"),
					   GetSQLValueString($_POST['GuAccDayTel'], "text"),
					   GetSQLValueString($_POST['GuAccMobTel'], "text"),
					   GetSQLValueString($_POST['GuAccEmail'], "text"),
					   GetSQLValueString($_POST['GuAccAdditional'], "text"),
                       GetSQLValueString($_POST['GuEmployerNam'], "text"),
                       GetSQLValueString($_POST['GuEmployerAdd'], "text"),
                       GetSQLValueString($_POST['GuEmployerContactName'], "text"),
                       GetSQLValueString($_POST['GuEmployerTele'], "text"),
                       GetSQLValueString($_POST['GuEmployerEmail'], "text"),
                       GetSQLValueString(isset($_POST['GuEmployerCCj']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString(isset($_POST['GuEmployerArr']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString(isset($_POST['GuEmployerBankR']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString(isset($_POST['GuEmployerNone']) ? "true" : "", "defined","'Y'","'N'"));

  mysql_select_db($database_hostprop, $hostprop);
  $Result1 = mysql_query($insertSQL, $hostprop) or die(mysql_error());

	// Auto Login User
	if (!isset($_SESSION)) {
		session_start();
	}

	$_SESSION['MM_Username'] = $_POST['userid'];
    $_SESSION['MM_UserGroup'] = "";	 
	
	// Redirect user to make-payment.php
	// With relevant POST Data
	$post_data = array("amount"=>250,"type"=>"deposit");
	$content_length = strlen($post_data);
	$_SESSION['post_data'] = $post_data;
	header("location: http://www.student.com/make-payment.php");
	exit();

}

mysql_select_db($database_hostprop, $hostprop);
$query_rsApplyNow = "SELECT * FROM host_editprop WHERE available = 0";
$rsApplyNow = mysql_query($query_rsApplyNow, $hostprop) or die(mysql_error());
$row_rsApplyNow = mysql_fetch_assoc($rsApplyNow);
$totalRows_rsApplyNow = mysql_num_rows($rsApplyNow);
?>


<form class="cmxform"action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
    <input type="hidden" name="MM_insert" value="form2" />


------all form content is on here---------

                <input type="submit" value="submit" />
     </form>
so basically if they cant complete the form i want another button to be added so the information is saved to the DB.....

When they start the form the have to provide a username and password, this can be used to log back in and complete the form when needed

thanks
Jon
Last edited by pickle on Mon Jun 11, 2012 11:22 am, edited 1 time in total.
Reason: Wrapping huge code block in syntax tags
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: form with two buttons one saving form other submit

Post by manohoo »

AJAX
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: form with two buttons one saving form other submit

Post by jonnyfortis »

ok well where do i start? do i have to build a wholew new form?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: form with two buttons one saving form other submit

Post by social_experiment »

jonnyfortis wrote:ok well where do i start? do i have to build a wholew new form?
A new form isn't necessary, just additions to your existing one. Your best option is to do a search for this type of thing using a search engine, an online tutorial will do a better job of explain the ideas behind the code than someone here just giving you the code :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: form with two buttons one saving form other submit

Post by jonnyfortis »

ok i will search for an online tutorial.

thanks for your time
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: form with two buttons one saving form other submit

Post by pickle »

AJAX isn't strictly necessary.

One option would be to have a second button on your form labelled "Save". On the page that the form is submitted to, you can check which button was used to submit the form, and act accordingly. If you have multiple <input type = "submit"> elements, only the one that was clicked will be sent in $_POST.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: form with two buttons one saving form other submit

Post by litebearer »

Is there any reason you couldn't just simply allow the user to edit their information later? ie save whatever data has been entered (makes no difference if complete or not) [perhaps add a field to the table "completed", where 1 is the form has been fully filled out and 0 is form needs more data] , then when they log in check the completed, if o - advise user and display form in editable mode
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: form with two buttons one saving form other submit

Post by jonnyfortis »

>>>>>>Is there any reason you couldn't just simply allow the user to edit their information later? ie save whatever data has been entered (makes no difference if complete or not) [perhaps add a field to the table "completed", where 1 is the form has been fully filled out and 0 is form needs more data] , then when they log in check the completed, if o - advise user and display form in editable mode.


yes that would be an idea but im not sure how i would implement the script
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: form with two buttons one saving form other submit

Post by jonnyfortis »

>>>>>>>>>>>>>>>>>>>>>>>>>>>AJAX isn't strictly necessary.

One option would be to have a second button on your form labelled "Save". On the page that the form is submitted to, you can check which button was used to submit the form, and act accordingly. If you have multiple <input type = "submit"> elements, only the one that was clicked will be sent in $_POST.


can you go into a bit more depth on what you mean because it sound like this is something that could work

thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: form with two buttons one saving form other submit

Post by pickle »

First off, there is a "quote" button beside everyone's reply that lets you quote their post - no need for >>>>>>>>>> (FYI)

What I meant was this:

Code: Select all

<form>
    <input type = "text" name = "first_name" />
    ... and so on ...
    <input type = "submit" name = "save" value = "Save form" />
    <input type = "submit" name = "submit" value = "Submit form" />
</form>
If someone clicks the "Save form" button, $_POST will have a "first_name" and "save" element, but not a "submit" element. Similarly if someone clicks the "Submit form" button, $_POST will have a "first_name" and a "submit" element, but not a "save" element.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: form with two buttons one saving form other submit

Post by jonnyfortis »

ok thanks i think i understand, will see what i can come up with

regards
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: form with two buttons one saving form other submit

Post by jonnyfortis »

pickle wrote:First off, there is a "quote" button beside everyone's reply that lets you quote their post - no need for >>>>>>>>>> (FYI)

What I meant was this:

Code: Select all

<form>
    <input type = "text" name = "first_name" />
    ... and so on ...
    <input type = "submit" name = "save" value = "Save form" />
    <input type = "submit" name = "submit" value = "Submit form" />
</form>
If someone clicks the "Save form" button, $_POST will have a "first_name" and "save" element, but not a "submit" element. Similarly if someone clicks the "Submit form" button, $_POST will have a "first_name" and a "submit" element, but not a "save" element.


when you say a "save element" do you mean there will be a save in the if statement and a save in the DB? aswell as all the other feilds? so there will be 2 if statement with all the other feilds and one with a save and one with a submit?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: form with two buttons one saving form other submit

Post by social_experiment »

If the 'save' button is clicked you will have $_POST['save'] in the $_POST array so you can test for it's presence like this

Code: Select all

<?php
 if (isset($_POST['save']) && $_POST['save'] == 'Save form') {
   // save button clicked, execute corresponding code
 }
?>
An easy method to see this in action is to do a var_dump() or print_r() of $_POST on the page that processes the script

Code: Select all

<?php
 print_r($_POST);
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: form with two buttons one saving form other submit

Post by jonnyfortis »

social_experiment wrote:If the 'save' button is clicked you will have $_POST['save'] in the $_POST array so you can test for it's presence like this

Code: Select all

<?php
 if (isset($_POST['save']) && $_POST['save'] == 'Save form') {
   // save button clicked, execute corresponding code
 }
?>
An easy method to see this in action is to do a var_dump() or print_r() of $_POST on the page that processes the script

Code: Select all

<?php
 print_r($_POST);
?>
i am confused where this would go. i have created the save and submit buttons on the page but i am unsure where to put the

if (isset($_POST['save']) && $_POST['save'] == 'Save form') {
// save button clicked, execute corresponding code
}

as i already have

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {

in the code, does your code site above this?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: form with two buttons one saving form other submit

Post by social_experiment »

jonnyfortis wrote:in the code, does your code site above this?
For arguments sake yes; below is a snippet of conditional code on how to check which button was clicked

Code: Select all

<?php
if (isset($_POST['save']) && $_POST['save'] == 'Save form') {
// save button clicked, execute corresponding code
}
else {
 // now the save button isn't clicked but you have to check if the 
 // submit button was clicked
 if (isset($_POST['submit']) && $_POST['submit'] == 'Submit form') {
     // submit button clicked, execute corresponding code
 }
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply