I am search the other result of related question and that i implement in my code but that not working ,i have 2 button in a form
my form
<form id="InVoice" action="<?php echo base_url('Staff/add_invoice_spare');?>" method="post" class="form-horizontal">
/*-- content herer--*/
<button style="float:right;" type="submit" name="ready" value="Ready For Bill" class="btn btn-danger" style="border-radius:60px;">Add to Ready for Bill</button>
<button style="float:right;margin-left:15px;" type="submit" name="print" class="btn btn-danger" style="border-radius:60px;">Print</button>
<?php echo form_close(); ?>
and used script is
<script type="text/javascript">
$(document).ready(function() {
$("form#InVoice").submit(function() {
alert();
$('button[type=submit]').prop('disabled',true);
return true;
});
});
</script>
this my controller for inserting
public function add_invoice_spare()
{
$bill=$this->input->post('ready');
if($bill)
{
$reg=$this->input->post('rno');
$data=array(
/*-----------datas here-----*/
);
$ans=$this->Bill_model->check_registration($reg);
$form_data= $this->input->post();
$result=$this->Bill_model->invoice_dtls_ready($form_data,$data);
if($result){
redirect('Staff/list_invoice/'.$result);
}
}else{
$reg=$this->input->post('rno');
$data=array(
/*-----------datas here-----*/
);
}
$form_data= $this->input->post();
$result=$this->Bill_model->invoice_dtls($form_data,$data);
if($result)
{
?>
<script type="text/javascript" language="Javascript">
window.open('invoice_pdf/<?PHP echo $result ?>', '_blank');
window.location.href = "invoice_labour";
</script>
<?php }
}
}
but in double click or single click the Add to Ready for Bill button the print button is work why that redirect?
any way to solve this issue and only one time submitte the data ?
thanks in advance!!!
Both the buttons that you are clicking on are of the type submit or they are submit buttons. So the only way to differentiate this will be at the backend.
For eg, according to the above example when you submit the $_POST array will have the submit button value. It is based on this value that you have to build your code logics.
If you click on Print then $_POST['submit'] will be the value will be print and when you click on "Add to Ready for Bill" the $_POST['submit'] value will be ready.
If you want to control it in the Frontend i.e., jQuery end you can by using the preventDefault (the example of which is given by #pradeep) but I suggest you modify the Backend as well to differentiate between these 2 submit buttons.
Hope this helps.
Related
My code:
<?php
if($counter==0) {
echo "value of counter = $counter"
?>
<script type='text/javascript'>
document.getElementById("tt").style.display = "none";
</script>
<?php
}
?>
<html>
<body>
<form action="new_upload.php" method="post" name="myform" enctype="multipart/form-data" id="formform">
<input type='submit' value='Go Back!' name="sbmt" id="tt"
<?php if($counter==0) { ?> display="none" <?php } ?> />
</form>
<p> Submit a different file by clicking the back button in case your file was unable to upload.</p>
</body>
</html>
You can see I have tried to set the property display=none of the submit button through
1) if php condition is true, I added javascript.
2) I added php in my form submit button code.
but it is not working both ways. I can see through the print statement that the value of counter is 0 but then also it is visible.
You are executing the script before the element is created. Move the code below the element to work
There is no such attribute as display, you need to use style="display: none" instead.
I have a small problem loading a php function if(isset... to a page where is form with onsubmit function :
function functionlol() {
$('#phptestlol').load('system/phptest.php');
}
Now, the phptest.php contains this :
if(isset($_POST['test'])){
echo "Working with submission also";
}
echo "Working";
and html with form looks like this:
<span id='phptestlol'></span>
<form method='POST' onsubmit='functionlol()' target='test'>
<input type='submit' name='test' value='Submit'>
</form>
I did target='test' because I don't want page to be refreshed.
When I click the submit button, it loads the php file, but it shows only the "Working" echo, and doesn't echo that "Working with submission also"... what am i supposed to do to make it work? Thanks!
EDIT
WHOLE HTML:
<html>
<head>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script>
$('form').submit(function(event) {
event.preventDefault();
$('#phptestlol').load('system/phptest.php', {test:1});
});
</script>
</head>
<body>
<iframe name='test' style='display:none;'></iframe>
<span id='phptestlol'></span>
<form method='POST' target='test'>
<input type='submit' name='test' value='Submit'>
</form>
</body>
</html>
PHP (phptest.php):
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
if(isset($_POST['test'])){
echo "submitted";
}
?>
Let's make some adjustments and take advantage of jQuery all the way through. First the HTML -
<span id='phptestlol'></span>
<form method='POST' target='test'>
<input type='submit' name='test' value='Submit'>
</form>
Note the removal of any inline JavaScript. Now for the PHP -
if(isset($_POST['test'])){
echo "submitted";
}
Now for the cherry on top of the cake, the jQuery
$(document).ready(function() {
$('form').submit(function(event) {
event.preventDefault(); // prevents the default action of the submit button
$('#phptestlol').load('system/phptest.php', {test:1});
});
});
Here we prevent the default action of clicking the submit button. We also send a value for the $_POST array ({test:1}) so the isset($_POST['test']) will work as we expect.
<?php for($i = 0; $i < $pin_num_rows; $i++){ ?>
<form action="/group/<?php echo $group_id ?>?Comment=<?php echo $pin_id[$i] ?>" method="post" id="group-comment-form">
<textarea name="group-comment" id="group-comment" placeholder="Add a comment..." spellcheck="false"></textarea>
</form>
<?php } ?>
This is the some part of my code. As you can see, I am passing the value of pin_id in my form action. The problem is, whenever I post comment, the form submits the value with /group/?Comment=1 but it supposed to submit with 1, when I comment to the first post. So, when I comment to the second post, it supposed pass 2 but it doesn't. It always passes /group/?Comment=1
I did echo this right after my textarea and I see that for every comment form, the number increments as it should be but when I submit, it submits with value 1. I am going crazy.
<?php echo "/group/<?php echo $group_id ?>?Comment=$pin_id[$i]" ?>
I have also this part in my code in the same loop above and it has the same logic but it works. I don't understand why the form part doesn't work.
Edit
Delete
EDIT
I wrote this simple program which demonstrates the same logic that I used for the site and this simple program works but website. I guess the problem is not about the names of form or textarea.
<?php
$inputValue = NULL;
$id = [1,2,3];
if(isset($_POST['inputName'])){
$inputValue = $_POST['inputName'];
echo "<br>Input Value: " . $inputValue;
}
if(isset($_GET['id'])){
$getValue = $_GET['id'];
echo "<br>Get Value: " . $getValue;
}
?>
<html>
<head><title>Multiple Forms in One Page</title></head>
<body>
<br>
Main Page
<br>
<?php for($i = 0; $i < 3; $i++){ ?>
<form action="index.php?id=<?php echo $id[$i] ?>" method="post" name="formName">
<textarea name="inputName"></textarea>
<input type="submit" name="submitName">
</form>
<?php } ?>
</body>
</html>
FOUND THE PROBLEM (BUT STILL NEED FIX)
<script>
$(function() {
$('textarea#group-comment').on('keydown', function(e) {
if(e.keyCode == 13 && !e.shiftKey){
document.getElementById('group-comment-form').submit();
}
});
});
</script>
I use this script to submit my forms. I don't have a submit button to submit. If I use submit button, everything works perfect but if I use this script above to submit the form, it won't work. What should I do to make the script above work?
SOLUTION
After I found where the problem is caused, I created another question to find a fix for the problem.
The answer is here: Solutution
In my webpage, I want to show all the items fetched form server, and under each item, their is a "Complete" button. When press the compete button, the id of the item will be send to a php file. The code for my form is:
<?php
$x = $NumberofItems;
while ($x > 0) :
?>
<p><i class="fa fa-question" style="color:#ffff00"></i> On <?php echo htmlspecialchars($date_Quest[$x])?>: <?php echo htmlspecialchars($desc_Quest[$x])?></p>
<form id="formQuestComplete" action="quest-complete.php" method="post" style="display: inline-block;">
<input type="hidden" name="QuestID" value="<?php echo $id_Quest[$x]; ?>"/>
<button id="subQuestComplete" class="btn btn-default btn-xs">Complete</button>
</form>
<?php
$x--;
endwhile;
?>
And my jquery code is :
$("#subQuestComplete").click( function() {
$.post( $("#formQuestComplete").attr("action"),
$("#formQuestComplete :input").serializeArray(),
function(info){ $("#result").html(info);
});
});
$("#formQuestComplete").submit( function() {
return false;
});
For example, when I have 10 items listed by the form, only the first item will send data and stay on the same page. All the rest will send data, but then redirect to the php page.
Change your PHP code to make sure item is given a class, instead of an id (because id's must be unique or they will not be handled correctly) -
<?php
$x = $NumberofItems;
while ($x > 0) :
?>
<p><i class="fa fa-question" style="color:#ffff00"></i> On <?php echo htmlspecialchars($date_Quest[$x])?>: <?php echo htmlspecialchars($desc_Quest[$x])?></p>
<form class="formQuestComplete" action="quest-complete.php" method="post" style="display: inline-block;">
<input type="hidden" name="QuestID" value="<?php echo $id_Quest[$x]; ?>"/>
<button class="subQuestComplete" class="btn btn-default btn-xs">Complete</button>
</form>
<?php
$x--;
endwhile;
?>
Then change your jQuery to handle the clicks appropriately (you do not have a submit, so eliminate the last function you show)-
$(".subQuestComplete").click( function(e) { // handle the button click
e.preventDefault(); // prevent the click's default action
var thisForm = $(this).parent('form'); // get this form
var thisData = $(this).parent('form').find('input').val();
$.post(
$(thisForm).attr("action"),
{QuestID: thisData}, // no need to serialize for a one value form
function(info){
$("#result").html(info);
});
});
Since the elements are now identified by class with can traverse the DOM tree to find the form value(s) for each form respctively - regardless of how many there may be in the page.
You have to modify your code like this:
.click( function(e) { e.preventDefault();
after this, the rest of your code.
If you dont do this, then the original click event handler also will run. after a time.
So your code should look like this:
$("#subQuestComplete").click( function(e) {
e.preventDefault();
$.post( $("#formQuestComplete").attr("action"), $("#formQuestComplete :input").serializeArray(),function(info){
$("#result").html(info);
});
});
Is it possible to isset() a hyperlink?
I can't seems to make it work. I don't know what to do; I looked for many solutions, but they used $_GET or $_REQUEST. I don't want to use those.
I want to make it work like a submit button I am new to PHP and here is what I have done:
<?php
session_start();
if (!isset($_SESSION['suser'])) {
header("location:register.php");
session_write_close();
exit;
}
error_reporting(E_ALL);
include 'conectthis.php';
$suser = $_SESSION['suser'];
mysql_select_db("$suser", $con);
if (isset($_POST['select'])) {
$result = "SHOW TABLES FROM $suser";
$show = mysql_query($result) or die(mysql_error());
if (!$show) {
echo "<script type='text/javascript'>alert('Create Master List First'); </script>";
exit;
}
}
?>
<body>
<hr />
<div align="center"><h1>Selection</h1></div>
<hr />
<form action="select.php" method="post">
<table width="50%" align="center">
<tr><td>Student Record </td><td><a name="select" href="fq1.php">First Period</a></td><td><a name="select2" href="fq2.php">Second Period</a></td><td><a name="select3" href="fq3.php">Third Period</a></td><td><a name="select4" href="fq4.php">Fourth Period</a></td><td><a name="summary" href="summary.php">Summary</a></td></tr></table>
</form></body>
I want a hyperlink to alert message something when the user have clicked a hyper link.
I hope you understand my English.
Thank you.
This is a shot in the dark, but is this what you want?
Show a message which is set through PHP.
Click Me
<script type="text/javascript">
function showMessage(message){
alert(message);
}
</script>
Show a message using just JavaScript.
Click Me
<script type="text/javascript">
function showMessage(){
alert('hello);
}
</script>
This would be done with JavaScript via a click event. PHP would have no part in it.