How to process two forms on a page in PHP - php

When processing two forms on a page how do I prevent Undefined Index for a variable that has not been submitted?
I have a page with two forms and I am trying to send one of the forms to the server, but I keep getting variables from the other form as an undefined index.
<form id="dassets" class="d-none" action="process.php" method="post" enctype="multipart/form-data">
<div>
<label class="medium my-2" for="assetType">Type</label>
<input class="form-control my-2" name="coinName" id="assetType" type="text" placeholder="Card Selected" /readonly>
</div>
<div class="col px-0">
<!-- Get current BTC value in dollars -->
<div id="dataBTC" class="text-primary"></div>
<label class="medium my-2">Coin value</label>
<div class="input-group input-group-sm mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="coin-value">BTC</span>
</div>
<input type="text" class="form-control" name="quantity" aria-label="BTC" aria-describedby="coin-value">
</div>
</div>
<div class="form-group">
<div class="col px-0">
<label for="file01" class="my-2">Please upload a very clear photo of the payment receipt/gift card where necessary</label>
<div class="form-group mt-4">
<label for="file01">Upload a file</label>
<input type="file" class="form-control-file" id="file01">
</div>
</div>
<input type="hidden" name="action" value="BTC">
<input type="submit" class="btn btn-success btn-rounded px-3 mt-3" value="Trade" name="submit"/>
</div>
</div>
<!-- Card trade -->
<div class="col-xl-7 card-body d-none" id="dock">
Here is the second form; I added a hidden field to distinguish them:
<form id="amazonForm" class="d-none" action="" method="post" enctype="multipart/form-data">
<div>
<label for="cardType">Type</label>
<input class="form-control" id="typeAmazon" type="text" placeholder="Card Selected" name="ca2rdType" readonly/>
</div>
<div class="col px-0">
<label for="ctry" class="my-2">Country</label>
<select id="ctryAmazon" class="form-control" name="country">
<option selected>USD</option>
<option>EUR</option>
<option>CAD</option>
<option>GDP</option>
<option>CHF</option>
<option>AUD</option>
</select>
</div>
<div class="col px-0">
<label for="country" class="my-3">Select Gift Card Type</label>
<!--Amazon Gift Cards -->
<div id="amazonUsa" class="d-none">
<!-- Cash Receipt -->
<div class="custom-control custom-checkbox ">
<input type="checkbox" class="custom-control-input" id="amazonCash">
<label class="custom-control-label" for="amazonCash">Cash Receipt</label>
</div>
<!-- Debit Receipt -->
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="amazonDebit">
<label class="custom-control-label" for="amazonDebit">Debit Receipt</label>
</div>
<!-- No Receipt -->
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="noReceipt">
<label class="custom-control-label" for="noReceipt">No Receipt</label>
</div>
<!-- Ecode -->
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="eCode">
<label class="custom-control-label" for="eCode">Ecode</label>
</div>
<!-- Physical -->
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="physical">
<label class="custom-control-label" for="physical">Physical</label>
</div>
<!-- Large card -->
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="largecard">
<label class="custom-control-label" for="largecard">Large Card</label>
</div>
</div>
<div class="form-group">
<div class="col d-md-flex justify-content-between px-0">
<div>
<label for="amount" class="my-3">Input card value amount</label>
<input class="form-control" id="amount" type="number" placeholder="Enter an amount"/>
</div>
<div>
<label for="equivalent" class="my-3">Equivalent in Naira</label>
<input class="form-control text-success" id="cashout" type="text" placeholder="Cashout in Naira" readonly/>
</div>
</div>
</div>
<div class="col px-0">
<label for="file02" class="my-2">Please upload a very clear photo of the payment receipt/giftcard</label>
<div class="form-group mt-4">
<label for="file01">Upload a file</label>
<input type="file" class="form-control-file" id="file02">
</div>
</div>
<input type="hidden" name="action" value="GTC">
<input type="submit" class="btn btn-success btn-rounded px-3 mt-3" value="Trade" name="submit_2"/>
</div>
</form>

Just use an if statement in your PHP to check if your variables exist before you use them, and if they are undefined then you can deal with that error in a cleaner fashion.

Give each submit button a different name i.e.
<input name='form1' type='summit' value='submit'>
<input name='form2' type='summit' value='submit'>
In php you can then use
if( isset($_POST['submit']) && $_POST['submit'] == 'form1' ) {
// do form1 stuff
} else if( isset($_POST['submit']) && $_POST['submit'] == 'form2' ) {
// do form 2 stuff
}

Try this at SERVER side (i.e. process.php):
if ($_POST['action'] == 'BTC') {
//action for BTChere
} else if ($_POST['action'] == 'GTC') {
//action for GTC
} else {
//invalid action!
}
Refer this answer
Still you have issue then try AJAX.
Also,
You miss action attribute from second form (it's empty).
If you want to submit to process.php then add in second form (No need to check action again)
or other file then add respective path (Need to check action i.e. $_POST['action'] is BTC or GTC).
By default,empty action submit redirect to same file.
Check this for details
Hope you get idea.

Can you show the php block, also are you checking isset("submit")? then since you know the particular action that was performed also check the isset the variables too.

Related

how can i disable a text field on radio click, enable text on another radio click and clear the three using a link

So I have a php form that I am creating and it has hundreds of fields.
If a radio input repair is selected I want to enter text in the text field.
If radio monitor is selected I want to disable the input text field.
If clear is clicked, I want to clear only the two radio buttons and disable the input text field
<h5>FL7 - </h5>
<div class="input-group m-b-15">
<div class="input-group m-b-15">
<span class="js-clear">Clear</span>
</div>
<div class="radio radio-danger form-check-inline">
<input type="radio" name="val-55" value="r" id="repair_55">
<label for="repair_55">Monitor</label>
</div>
<div class="radio radio-warning form-check-inline">
<input type="radio" name="val-55" value="m" id="monitor_55">
<label for="monitor_55">Repair</label>
<input name="comment_55" type="text" class="form-control" id="comment_55" >
</div>
</div>
<h5>FL8 - </h5>
<div class="input-group m-b-15">
<div class="input-group m-b-15">
<span class="js-clear">Clear</span>
</div>
<div class="radio radio-danger form-check-inline">
<input type="radio" name="val-56" value="r" id="repair_56">
<label for="repair_56">Monitor</label>
</div>
<div class="radio radio-warning form-check-inline">
<input type="radio" name="val-56" value="m" id="monitor_56">
<label for="monitor_56">Repair</label>
<input name="comment_56" type="text" class="form-control" id="comment_56" >
</div>
</div>
<h5>FL9 - </h5>
<div class="input-group m-b-15">
<div class="input-group m-b-15">
<span class="js-clear">Clear</span>
</div>
<div class="radio radio-danger form-check-inline">
<input type="radio" name="val-57" value="r" id="repair_57">
<label for="repair_57">Monitor</label>
</div>
<div class="radio radio-warning form-check-inline">
<input type="radio" name="val-57" value="m" id="monitor_57">
<label for="monitor_57">Repair</label>
<input name="comment_57" type="text" class="form-control" id="comment_57" >
</div>
</div>
You can do this using JavaScript instead of using php. Php requires server-side access and can not be executed on the client side alone which would make it useless for manipulating a form. Check out how to do this below with the example
The text input and the radio button
<input type="text" id="username" />
<input type="radio" id="radio_button" />
The javascript part
$("#radioButton").click(function(){
//disable the text field
document.getElementById('username').setAttribute('disabled ', 'disabled');
});
That should be able to help you

Padding and class col-md not working

I am trying to shift my radio button to the right for this certain part for a long time and it still doesn't work, I try using padding and class col-md for it and even check if my tag are closed but it is still not moving to the right. Can anybody help me with this problem? Thanks in advance.
test.blade.php
<div class="form-group">
<label class="col-md-2"><b>Residential Status:</b></label>
<div class="col-md-8">
<input type="radio" name="residential_status" value="Local" required> Local<br>
<input type="radio" name="residential_status" value="Overseas" required> Overseas<br>
</div>
</div>
<div class="form-group">
<div class="editfield1">
<div class="radio">
<div id="chkNRICid1">
<label style="font-weight: bold">Do you have a NRIC?</label> <br/>
<input type="radio" name="NRIC_check" id="nricid1" value="Yes" onclick="document.getElementById('NRIC_check').style.display='block'" required> Yes <br>
<input type="radio" name="NRIC_check" id="nricid2" value="No" onclick="document.getElementById('NRIC_check').style.display='none'" required> No
</div>
</div>
</div>
<div class="editfield1" id="NRIC_check" style="display:none">
<label for="NRIC_check"> Type your NRIC/FIN number:</label>
<label><input type="text" name="nric_number1" id="nric_number1"> </label>
</div>
</div>
This is the screenshot of what is happening: (I want it to be like the Residential Status)
So many wayward and un-needed divs you can get rid of along with fixing your nesting, after that it's just a matter of following the structure in the first part of your template:
<div class="form-group">
<div class="col-md-2">
<label>Residential Status</label>
</div>
<div class="col-md-10">
<label><input type="radio" name="residential_status" value="Local" required> Local</label>
<br />
<label><input type="radio" name="residential_status" value="Overseas" required> Overseas</label>
</div>
</div>
<div class="form-group">
<div class="col-md-2">
<label>Do you have a NRIC?</label>
</div>
<div class="col-md-10">
<label><input type="radio" name="NRIC_check" id="nricid1" value="Yes" onclick="document.getElementById('NRIC_check').style.display='block'" required>Yes</label>
<br />
<label><input type="radio" name="NRIC_check" id="nricid2" value="No" onclick="document.getElementById('NRIC_check').style.display='none'" required> No </label>
</div>
</div>
<div class="form-group" id="NRIC_check" style="display:none">
<div class="col-md-2">
<label>Type your NRIC/FIN number:</label>
</div>
<div class="col-md-10">
<input type="text" name="nric_number1" id="nric_number1" />
</div>
</div>
Wrap the two inputs inside a <div> and add some margin.
<div style="margin-left: 50px">
<input ... />
<input ... />
</div>
If you are using Bootstrap 4 you could even:
<div class="ml-5">

If I click on one check box another check box also checked

If I click on one check box another check box also checked.
addquiz.php
<?php
$unique_id = time().'_'.uniqid(true);
$userid = $this->session->userdata('cp_userid');
?>
<!-- DC QUIZZ -->
<form class="addquizform" id="quiz_form_<?php echo $unique_id;?>" data-form = "<?php echo $unique_id;?>" data-user = '<?php echo $userid ?>'>
<input type="hidden" name="sid" value="<?php echo $sectionId;?>">
<input type="hidden" name="cid" value="<?php echo $courseId;?>">
<div class="dc-quizz-info dc-course-item">
<div class="dc-content-title">
<h5 class="xsm black">Quiz : Title of quiz</h5>
<div class="course-region-tool">
<input type="button" value = "Save" class="mc-btn-3 btn-style-1 savequiz save" id="save_quiz_<?php echo $unique_id;?>">
</div>
</div>
<div class="quizz-body dc-item-body">
<div class="form-item form-checkbox checkbox-style">
<input type="checkbox" id="showanswer" value="1" name="showanswer">
<label for="showanswer">
<i class="icon-checkbox icon md-check-1"></i>
Show Answer
</label>
</div>
<div class="form-item form-checkbox checkbox-style">
<input type="checkbox" id="limittime">
<label for="limittime">
<i class="icon-checkbox icon md-check-1"></i>
Limit time
</label>
</div>
<div class="time">
<div class="form-item">
<input type="text" name="limittime">
<label for="">mins</label>
</div>
</div>
<div class="form-item">
<input type="text" placeholder="Quiz Title" class="fullwidth" name="quiztitle">
</div>
<div class="form-item">
<input type="text" placeholder="Short introduction" class="fullwidth" name="quizintro">
</div>
</div>
</div>
</form>
<!-- END / DC QUIZZ -->
when I go to add one more quiz it asks to check the check box, but when I clicked on check box above check box also checked. How to over come this problem.
Please Check The Name as well as id You Defined there is missing an id and name for checkbox
<div class="dc-quizz-info dc-course-item">
<div class="dc-content-title">
<h5 class="xsm black">Quiz : Title of quiz</h5>
<div class="course-region-tool">
<input type="button" value = "Save" class="mc-btn-3 btn-style-1 savequiz save" id="save_quiz_<?php echo $unique_id;?>">
</div>
</div>
<div class="quizz-body dc-item-body">
<div class="form-item form-checkbox checkbox-style">
<input type="checkbox" id="showanswer" value="1" name="showanswer">
<label for="showanswer">
<i class="icon-checkbox icon md-check-1"></i>
Show Answer
</label>
</div>
<div class="form-item form-checkbox checkbox-style">
<input type="checkbox" name="limitcheck" id="limitcheck">
</div>
<div class="time">
<label for="limittime">
<i class="icon-checkbox icon md-check-1"></i>
Limit time
</label>
<div class="form-item">
<input type="text" value="" id="limittime" name="limittime">
<label for="">mins</label>
</div>
</div>
<div class="form-item">
<input type="text" placeholder="Quiz Title" class="fullwidth" name="quiztitle">
</div>
<div class="form-item">
<input type="text" placeholder="Short introduction" class="fullwidth" name="quizintro">
</div>
</div>
</div>

How to save data and print form at one click in php?

I am working on invoice where I have to save form's data in database and at the same time it should be print. So, I have 3 pages: invoice.php, invoice_print.php and insert_data.php
invoice_print.php is a html form that should be printed.
Now, user will go to first invoice.php then he will fill details and either he will click on submit button to save data in db or else he will click on print button to print that invoice.
Now, lets come to the second part: If user will select print button then data will go to database first and then it will go to invoice_print.php with same data that he filled.
How to do this? What logic should I use to save data and capture that data's id in button and then send that id on another page to display?
invoice.php:
<form id="demo-form2" action="insert_data.php" method="post" data-parsley-validate class="form-horizontal form-label-left">
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Location</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="designation">
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Address</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="contact">
</div>
</div>
</form>
invoice_print.php:
<div class="col-md-12">
<div class="col-md-6">
<h2>DIGILIFE BIZCARE SOLUTIONS</h2>
<p>414, Vashi Infotech Park,Maharashtra</p>
</div>
<div class="col-md-6">
<h2>BILL OF SUPPLY</h2>
</div>
</div>
<div class="col-md-12">
<div class="col-md-6">
<div class="col-md-12">
<div class="col-md-6">
<label>GSTIN</label>
<input type="text" name="gstin" value="">
<label>Serial No & Date of Invoice</label>
<input type="text" name="serialNo">
</div>
<div class="col-md-6">
</div>
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
<div class="col-md-6">
<label>Mode of Transport</label><br>
<label>Vehicle No</label><br>
<label>Date & Time of Supply</label><br>
<label>Place Of Supply</label>
</div>
<div class="col-md-6s">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
</div>
</div>
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Location</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="designation">
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Address</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="contact">
</div>
</div>
I would put all of the code for a form into one file, structured thus:
<?php
$formDone=!empty($_POST['formDone']);
$printReq=!empty($_POST['printReq']);
if ($formDone)
{
// perform validation
}
if ($formDone && $validationPassed)
{
// write to database using $_POST data
}
if ($formDone && $databaseWriteSuccess && $printReq)
{
// print using $_POST data
}
// end of PHP
?>
<form method="post" action="<?php echo $PHP_SELF ?>">
<fieldset>
<input type="hidden" name="formDone" value="1" />
</fieldset>
<!-- field forms -->
<input type="submit" value="Save to db" />
<input type="submit" name="printReq" value="Print Invoice" />
</form>

Passing Values from One Form to another redirected Form in PHP

<form class="form-horizontal" enctype="multipart/form-data" method="post" action="store.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No.</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
Here, in this code there are more input types, I just skipped those. When I will press submit button this will take me to 'store.php' file where informations will be stored in database. And redirect me to another form and there are also some input type but I need a information like "reg_no" from the previous form in the current form. How can I get this?
NOTE : I am storing the inputs of first form in a database table. Then redirected to other form and the action of this form is another PHP file like 'store2.php' and it is also using to store informations to other table of database.
You could pass the value of reg_no as a
$_SESSION Variable
to the second form and get it there
You said that you are already passing the information from your current form into a database. When rendering your second form, you could grab whatever value you need from your database, and then add a value attribute to your input.
So for example if you have a form that looks like this in your second form:
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
You can add that class to Pre-Fill the form when the page loads. Lets say you want it to show "12345":
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input" value="12345">
I don't know the details of exactly how you are serving your site through PHP, but you could use some kind of string placeholder, or templating engine to put in this attribute.
Maybe this can help you :
this called form.php :
<form class="form-horizontal" enctype="multipart/form-data" method="POST" action="form_p.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No.</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
this process of form.php, called form_p.php :
<?php
$reg_date = $_POST['reg_date'];
$reg_no = $_POST['reg_no'];
$submit = $_POST['submit'];
if (isset($submit)) {
$url = 'form_next.php?reg_date=' . $reg_date . '&reg_no=' . $reg_no;
header('Location:' . $url);
}
this next form, called form_next.php :
<?php
$reg_date = $_GET['reg_date'];
$reg_no = $_GET['reg_no'];
?>
<form class="form-horizontal" enctype="multipart/form-data" method="POST" action="form_p_next.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE AGAIN</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input" value="<?php echo $reg_date; ?>">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No. AGAIN</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input" value="<?php echo $reg_no; ?>">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
and this is process of form_next.php, called form_next_p.php
<?php
//define your code
The code work because I passing reg_date and reg_no from form_p.php to form_next.php and I use $_GET to get reg_date and reg_no and print it in input value.
This is help you?

Categories