Create button with two functions - php

I downloaded some code that uses the CodeIgniter framework, and I have the confirm now button which confirms the appointment. I would like to add the following code to the button so that it will confirm my appointment as well as take the user to the payment gateway.
Payment gateway code:
<a href="https://www.payfast.co.za/eng/process?cmd=_paynow&receiver=10907075&item_name=Tutor+Session&item_description=Tutor+Meetup&amount=170.00&return_url=https%3A%2F%2Fwww.lectiotutoring.co.za&cancel_url=https%3A%2F%2Fwww.lectiotutoring.co.za"><img src="https://www.payfast.co.za/images/buttons/light-small-paynow.png"
width="165"
height="36"
alt="Pay"
title="Pay Now with PayFast" />
</a>
Existing button with function i want to add the above code to:
<form id="book-appointment-form" style="display:inline-block" method="post">
<button id="book-appointment-submit" type="button" class="btn btn-success">
<span class="glyphicon glyphicon-ok"></span>
<?= !$manage_mode ? lang('confirm') : lang('update') ?>
</button>
<input type="hidden" name="csrfToken" />
<input type="hidden" name="post_data" />
</form>

Its not so much the button but rather the logic you put on the event.
you first should validate success of appointment confirmation. If success is true, then you can redirect to payment gateway.
you can do this two ways from php side using header 301 or with ajax and javascript

Related

Forced to click submit button twice

I have Laravel 5.5 a page that has two forms but uses the same controller and method. First form is to cater for initial details but the second form is a search form. My search form works but only if you click the search button twice is there a way I could force to click once to submit that form.
View
<form name="FormSearch" id="FormSearch" method="post" action="{!! action(MyController#index',$customID) !!}">
<input type="text" name="searchDets" id="searchDets" value="" />
<input type="submit" class="btn btn-default" name="searchMe" id="searchMe" value= "Search Me"/>
</form>
Js
$('#FormSearch').click(function(e){
e.preventDefault();
$('#filterSearchForm').submit();
});
I would like my view page to submit once.
First of all, I couldn't find the element with id filterSearchForm, so, here is an edit that I made that submits the same form (i.e. FormSearch) when you click on it:
$('#FormSearch').click(function(e){
//e.preventDefault();
$('#FormSearch').trigger('submit');
});
I have used the trigger event that is fired which submits the form FormSearch when you click on the form FormSearch.
Here is it how it works:
$('#FormSearch').click(function(e) {
//e.preventDefault();
$('#FormSearch').trigger('submit');
});
$("#FormSearch").submit(function(e){
e.preventDefault();
alert('submitted');
});
#FormSearch{
background: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="FormSearch" id="FormSearch" method="post" action="{!! action(MyController#index',$customID) !!}">
<input type="text" name="searchDets" id="searchDets" value="" />
<input type="submit" class="btn btn-default" name="searchMe" id="searchMe" value="Search Me" />
</form>
<p>
** Click anywhere on the black area.
</p>
I hope this was helpful.
I have faced the same issue and it was due to js version . So please check your jquery.validate's version.
You can get more information here submit button twice click issue

php check the button is set and show some message

I have a markup like this
<button type="button" class="btn btn-success pull-right add-customer" name="add-new-customer">Add New Cusomer</button>
so when the add new customer will be clicked it should show some message. like this
<?php
if(isset($_POST['add-new-customer'])) {
echo 'Set';
}
?>
but it is not doing isset for the button. So can somone tell me how to solve this using php. I have not used any kind of form. I want to simply check the button is set and show some message. Any help will be really appreciable. Thanks
This is because if you click a button within a form it doesnt actually submit the form.
Try using
<input type="submit" value="Submit" name="add-new-customer" />
EDIT:
Having just seen your comment, you must wrap the elements in a
<form>
Button elements aren't linked to anything in HTML. PHP won't detect if you click on a button.
You have to use a form or an AJAX query in order to populate the $_POST variable.
<form action="" method="post">
<input type="submit" value="Submit" name="add-new-customer" />
</form>
button type must be submit like type="submit" not type="button"
<button type="submit" class="btn btn-success pull-right add-customer" name="add-new-customer">Add New Cusomer</button>
also use form tag

how to check which button pressed in php

i got following code i want to know which button pressed then pass the value to input box.
<button type="button" name="buttonpassvalue" value="1" onclick="">Value1</button>
<button type="button" name="buttonpassvalue1" value="2" onclick="">value 2 </button>
<?php
if buttonpassvalue pressed then add the buttonpassvalue value
<input type="text" name="value">
else
add value of buttonpassvalue1
?>
i am tried to solve but stock here.
please help me
thanks
The best way to do this is with Javascript.
As PHP is a server side language, it requires you to send some information to the server, meaning you would have to submit the form, and reload the page with the details of the request from the user.
With a javascript library like jQuery you can do something like the following.
<button class="some-button" value="1">Button 1</button>
<button class="some-button" value="2">Button 2</button>
<input type="hidden" name="buttonValue" class="button-value-hidden" />
<script>
$(document).ready(function(){
$('button.some-button').on('click', function(){
$('input.button-value-hidden').val($(this).val());
});
});
</script>
Now $_GET['buttonValue'] will contain your button value when the form is submitted.
Make sure you are including the jQuery library!

Why does my "add to cart" button not work?

My website uses a private e-commerce system, and unfortunately, while their support team are very helpful, they are not particularly supportive of 'custom code', such the code that I require.
I am trying to configure a "splash" page (which pops up before users complete their order), which offers users the opportunity to buy one of our t-shirts half-price, with an "add to cart" allowing them to add it to their cart from within the splash page.
I have tried copying code from the standard product pages, but unfortunately the button does not work as it should, and instead it simply links through to '#' (which can be seen in the code below).
<form action="includes/cart/add_to_cart.inc.php" method="get" name="addcartform" id="addcartform" onsubmit="return false;">
<!-- Important hidden inputs -->
<input type="hidden" id="js" name="js" value="false" />
<input type="hidden" id="product_id" name="product_id" value="{product:id}" />
<a id="add_to_cart" style="display:none;" href="#" onclick="{product:add_to_cart_js}" class="generated_button" title="Add To Cart" >
<img src="images/icons/cart_add.png" align="absmiddle" />
<span> Add To Cart </span> </a>
<noscript>
<!-- non javascript -->
<input class="generated_button" type="submit" value="Add To Cart" name="submit" id="cart_submit" />
</noscript>
After putting the code in, I change the "display" of the link to "inline-block" (so that it shows), and I also tried putting in the ID of the product that I want users to add to their cart here:
id="product_id" name="product_id" value="{product:id}"
Unfortunately none of this has worked, and as I dont have complete (or easy) access to all of the files I need, I'd really appreciate any help with getting this one resolved!
Thanks in advance,
Dan
UPDATE: The support team behind our eCommerce system have since sent me this piece of code to help, but I'm not entirely sure how to use it:
If you want more control via javascript you can use the core Cart object we make available > in the javascript. Its accessable via the (Private eCommerce System) namespace, and in
fact if you open up the console in an (Private eCommerce System) site and type in (Private > eCommerce System) and hit enter it will return an object to you of what parts of the core > system we make available to you right in the javascript so far, (this will increase in
time too).
The (Private eCommerce System).Cart object has a sub addItem() method/function that can be used here.
You can pass it the product id to add it to cart: (e.g. adding a product with id #52)
Cart.addItem(52);
Or you can pass it these full options like so:
Cart.addItem({
productId: 52,
qty: 1,
options: {},
wishlistId: 0,
extraData: "Some detail i want to show to the user"
}, function () {
console.log('This is the callback for when its complete!');
});
So if they add a button or link, they can use jQuery to set on click of that button to run that code, e.g.
jQuery(function ($) {
$('.some-button').on('click', function () {
Cart.addItem(52);
return false;
});
});
What if you swap out the Javascript button and replace it with the no-script input field?
<form action="includes/cart/add_to_cart.inc.php" method="get" name="addcartform" id="addcartform" onsubmit="return false;">
<!-- Important hidden inputs -->
<input type="hidden" id="js" name="js" value="false" />
<input type="hidden" id="product_id" name="product_id" value="{product:id}" />
<input class="generated_button" type="submit" value="Add To Cart" name="submit" id="cart_submit" />
<img src="images/icons/cart_add.png" align="absmiddle" />
<span> Add To Cart </span>
</form>
You might need to re-style the form fields and the <span> which says "Add to Cart" to get it looking right...
UPDATE:
Given the information the support team have provided, try putting this in your "splash page", instead of the form above.
<div>
<script>
function addTshirtToCart(){
/* Swap out the 34 for the actual product ID of the tshirt you want to
add to the basket */
Cart.addItem(34);
// You might also want some code here to close the "splash page"
return false;
}
</script>
<a href="#" id="add_to_cart" onclick="return addTshirtToCart();">
<img src="images/icons/cart_add.png" align="absmiddle" />
<span> Add To Cart </span>
</a>
</div>
UPDATE 2:
Replace the existing div from my 2nd answer and replaced it with this which works for me:
<form action="includes/cart/add_to_cart.inc.php" method="get" name="addcartform" id="addcartform" onsubmit="return false;">
<input type="hidden" id="js" name="js" value="false">
<input type="hidden" id="product_id" name="product_id" value="1618">
<a id="add_to_cart" style="" href="#" onclick="update_cart_content(); return false;" class="generated_button" title="Add To Cart">
<img src="images/icons/cart_add.png" align="absmiddle">
<span> Add To Cart </span> </a>
<noscript>
<input class="generated_button" type="submit" value="Add To Cart" name="submit" id="cart_submit"/>
</noscript>
</form>
UPDATE 3
To close the popup when adding the t-shirt to the cart, try putting this in the a element above:
onclick="update_cart_content(); spashBox.close(); return false;"

Confirm/redirect functionality not working - javascript

Here I used JavaScript to Delete an employee...
<script type="text/javascript">
function delet(emp)
{
var answer = confirm('Are you sure?');
if(answer)
{
window.location='delete.php?emp='+emp;
}
}
</script>
By using anchor tag am calling the function of javascript ....
<a onclick="javascript:delet('<? echo $_GET['emp']; ?>')">
<input type="button" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value=""/></a>
But my problem is it is working upto showing the alert msg but after answering the alert msg it is not redirecting to the page given by me that is "delete.php?emp=+emp"
Because you're nesting an <input type="Submit"/> inside an <a>, clicking the button is not the same as clicking the <a>, rather it "Submits" nowhere (read to the page you're already on, refreshing the page) before the <a> can do it's job.
Simple demo where you don't go to google.
<input type="Submit"/>
Bergi has pointed out that the behaviour I described is not universal (hello Opera, IE) unless a <form> element is present, so for example the following
<form action="jail.php">
<a href="go.php" onclick="window.location='?collect=£200';">
<input type="submit"/>
</a>
</form>
will send you directly to jail without passing go or collecting £200 across all browsers.
Further, this only really applies to page redirection; other pieces of script may well fire before the page changes, the easiest to observe being console.log, alert, etc.
You're submitting some form by clicking on that button. That submit process will overrule the window.location navigation request. A quick workaround would be to prevent that:
<a onclick="event.preventDefault(); delet('<? echo $_GET['emp']; ?>');">
// cross-browser-safe:
<a onclick="delet('<? echo $_GET['emp']; ?>'); return false">
but actually your markup with the nested button is quite odd. You don't need that link at all, just use
<form action="delete.php" onsubmit="return confirm('Are you sure');">
<input type="hidden" name="emp" value="<? echo $_GET['emp']; ?>" />
<input type="submit" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value="" />
</form>

Categories