Button onClick not redirecting - php

SO..i have this project that is totally backwards, but basically i have to give a working form that adds a quantity of some item_ID and then redirects to the cart page of a framework i use.
Of course rather than me just skin my framework, and customize its functionality for him. This guy has some hacked up php pages that he "wrote himself" that he insists on hard coding forms into.
anyways, i have a form that submits to my cart.php in the action
<form name="orderform" method="post" action="http://s429036015.onlinehome.us/cart.php" id="orderform">
<input type="hidden" name="mode" value="add" />
<input type="hidden" name="productid" value="140" />
<input type="hidden" name="cat" value="" />
<input type="hidden" name="page" value="" />
Qty <input type="text" id="product_avail" name="amount" />
<button class="button main-button list-button add-to-cart-button" type="submit" title="" onClick="location.href='cart.php'">
<span class="button-right"><span class="button-left">Add to cart</span></span>
</button>
</form>
The above form submits properly, with the quantity entered, session stores the data and the items are in the cart just fine....however, that onclick event on the <button> doesn't work!
I've even tried this
<button class="button main-button list-button add-to-cart-button" type="submit" title="" onClick="GotoPage(); return:true;">
with this script ahead of the form
<script type="text/javascript">
function GotoPage() {
window.location = "http://s429036015.onlinehome.us/cart.php";
}
</script>
anyone have any ideas? driving me nuts...

In PHP, after you process data from form, try this:
header('location: http://s429036015.onlinehome.us/cart.php');

Related

Post woocommerce total to payment processor

I've been given the task of setting up a payment processor for my boss who uses Moneris in Canada. I need to use their form to post the price but unsure what the best method is and have little to no back-end WordPress experience.
I need to assign the woocommerce cart total to the input "charge_total" value on the form submit.
Sample Form:
<form action="https://esqa.moneris.com/HPPDP/index.php" method="POST"><input name="ps_store_id" type="HIDDEN" value="XXXX" />
<input name="hpp_key" type="HIDDEN" value="XXXX" />
<input name="charge_total" type="HIDDEN" value="1" />
<input name="SUBMIT" type="SUBMIT" value="Click to proceed to Secure Page" />
</form>
You need to modify your template where the form is generated.
your sample form would become
<?php
global $woocommerce;
$cartTotal=$woocommerce->cart->get_cart_total()
?>
<form action="https://esqa.moneris.com/HPPDP/index.php" method="POST">
<input name="ps_store_id" type="HIDDEN" value="XXXX" />
<input name="hpp_key" type="HIDDEN" value="XXXX" />
<input name="charge_total" type="HIDDEN" value="<?php echo $cartTotal?>" />
<input name="SUBMIT" type="SUBMIT" value="Click to proceed to Secure Page" />
</form>
wp-content/plugins/woocommerce/templates/checkout/form-checkout.php is your main checkout file. i do not know where in the checkout process you wish to allow the user to leave the site, but i assume it would be form-pay.php
Good day, hope it helps you out
edit:
after conversation in comments, i would point you towards using the hooks. i will be using the after checkout hook to inject some html in there but if you let me know where you need the code ill re edit the hook for the proper position
put this in the functions.php file in your enabled theme
function add_custom_html() {
global $woocommerce;
$cartTotal=$woocommerce->cart->get_cart_total()
$my_custom_form = '<form action="https://esqa.moneris.com/HPPDP/index.php" method="POST">
<input name="ps_store_id" type="HIDDEN" value="XXXX" />
<input name="hpp_key" type="HIDDEN" value="XXXX" />
<input name="charge_total" type="HIDDEN" value="'&$cartTotal&'"/>
<input name="SUBMIT" type="SUBMIT" value="Click to proceed to Secure Page" />
</form>';
echo $my_custom_form;
}
add_action( 'woocommerce_after_checkout_form', 'add_custom_html');

Submit form in two different ways

I have a form with two "a" links. Both should submit form, but in two different ways.
This is html I currently have.
HTML:
<form method="post" id="doc_form" target="somwhere">
<textarea name="doc_text"><?php echo $file; ?></textarea>
<input type="hidden" name="submitted" value="1" />
</form>
<a id="doc_send_email" class="btn">SEND</a>
<a id="pritn_pdf_btn" onclick="document.getElementById('doc_form').submit();" class="btn">Print <span class="span_pdf">PDF </span></a>
When user clicks on #pritn_pdf_btn, then the form is submitted to index.php with target="somwhere". On server side I am generating pdf with value from textarea and this PDF is opened in new tab (target="somwhere" is some trick that opens pdf in new tab).
However, now I also need submit form on #doc_send_email click. In this case I also need to generate PDF, but it should be sent to provided email and then it should show confirmation message on the same page. So:
In index.php I need somehow distinguish what caused form submission (pritn_pdf_btn) or (doc_send_email). If I would be able to set some variable on #pritn_pdf_btn.click and on #doc_send_email.click and then sent it via POST, it would help. But I could not find sollution.
I need some way to submit form with target="somwhere" and without target="somwhere". Probably, there is someway on the #pritn_pdf_btn.click and on #doc_send_email.click to submit form with and without target?
It's easier to do it with multiples submit buttons like this:
<form method="post" id="doc_form" target="somwhere">
<textarea name="doc_text"><?php echo $file; ?></textarea>
<input type="hidden" name="submitted" value="1" />
<input type="submit" name="email" value="1" id="doc_send_email" class="btn" value="SEND"/>
<input type="submit" name="pdf" value="1" id="pritn_pdf_btn" class="btn" value="Print PDF"/>
</form>
Then at backend (asuming it is PHP)
if($_POST['email'])
sendEmail();
else
generatePDF();
Or by using JS:
<form method="post" id="doc_form" target="_self">
<textarea name="doc_text"><?php echo $file; ?></textarea>
<input type="hidden" name="submitted" value="1" />
<input type="hidden" name="action" id="action" value="generate" />
</form>
<a id="doc_send_email" onclick="document.getElementById('action').value='email';document.getElementById('doc_form').submit();" class="btn">SEND</a>
<a id="pritn_pdf_btn" onclick="document.getElementById('action').value='generatePdf';document.getElementById('doc_form').target='somwhere';document.getElementById('doc_form').submit();" class="btn">Print <span class="span_pdf">PDF </span></a>
Then at backend (asuming it is PHP)
if($_POST['action']=='email')
sendEmail();
else
generatePDF();

2 Forms in a page, Get the Value from the other form

I have 2 HTML form in a page
<form action="my-page.php" method="post" id="savedata" name="savedata">
<input class="text" name="myname" value="" type="text" />
<input class="text" name="myaddress" value="" type="text" />
<input type="submit" value="Save" />
</form>
<form action="my-page.php" method="post" id="previewdata" name="previewdata">
<input type="submit" value="Preview" />
</form>
The first one [savedata] will save the data to MySQL after clicking the [Save Button]
The second one [previewdata] will preview (means will just show in the page using HTML) the data once the [Preview Button] was clicked
How can the 2nd form get the data from the 1st form?
As an alternative to using 2 different forms you could have a single form with 2 submit buttons:
<form action="my-page.php" method="post">
<input class="text" name="myname" value="" type="text" />
<button type="submit" name="action" value="save">Save</button>
<button type="submit" name="action" value="preview">Preview</button>
</form>
Now inside your server side script look for $_POST["action"] and act accordingly.
Also in HTML5 there's the formaction attribute that could be specified on a submit button to invoke a different server side script.

Different POST Options for form, then passing data

Currently, I am working on a site that uses PayPal to Checkout. I would like to enable stripe, as well, so there can be a "checkout with stripe" button at the bottom of the form.
<form method="POST" id="form1" >
<select id="udidSelect" onchange="udidPrice(this)">
<option value="invalid" disabled="disabled">Choose Package</option>
<option value="udid">UDID Registration</option>
<option value="profile">UDID Registration & Free Unlimited Apps (Provisioning Profile)</option>
</select><br />
<br />
<input type="text" placeholder="Name" /><br />
<br />
<input type="email" placeholder="Email" /><br />
<br />
<input type="text" placeholder="UDID (40 Characters Long)" name="os0" /> <br />
<input type="hidden" name="on0" value="UDID">
<br />
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" id="udidbuttonID" value="9PSTCESRS3FSG">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" value="PayPal" onclick="submitForm('https://www.paypal.com/cgi-bin/webscr')" style="float:right;">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
Now I would like to have a PayPal Button at the bottom of the form and a Stripe button at the bottom of the form. And if the user clicked the stripe button, it would take them to the next page and PASS ALONG the information entered in this page. I am not sure how to do this, and I am also not sure how to have different post options for the form. (Sorry I am kind of new to this.)
On the stripe checkout page, also, how would I request that information that was passed along, from the previous form. Would it just be something like this?
<input type="hidden" name="somedata" value="<?php echo $_POST['somedata']; ?>" />
<input type="hidden" name="otherdata" value="<?php echo $_POST['otherdata']; ?>" />
If I'm reading your question correctly, you want the form to go to different pages depending on whether the user clicked on Stripe or Paypal.
You can use JavaScript/jQuery to change the action attribute of the form:
<!--HTML-->
<button onClick="setFormAction('stripe')">Stripe</button>
<button onClick="setFormAction('paypal')">Paypal</button>
//Javascript
function setFormAction(which) {
if (which == 'stripe') {
//Change 'stripe.php' to the proper URL
document.getElementById('form1').action = 'stripe.php';
} else {
document.getElementById('form1').action = 'paypal.php'; //Change this also
}
//Finally, submit the form
document.getElementById('form1').submit();
}
Or, more understandably, the jQuery solution:
<!--HTML-->
<button id="stripe">Stripe</button>
<button id="paypal">Paypal</button>
//Javascript
$('button#stripe').click(function() {
$('form#form1')
.attr('action', 'stripe.php') //Change to proper stripe URL
.submit();
});
$('button#paypal').click(function() {
$('form#form1')
.attr('action', 'paypal.php') //Change to paypal URL
.submit();
});
On the next page, you do exactly what you said previously:
<input type="hidden" name="somedata" val="<?php echo $somedata; ?>" />
Always sanitize user-inputted values before echoing them on the page (therefore the variable $somedata rather than $_POST['somedata'].
An alternative to hidden input fields is sessions. Much easier to handle once you get the hang of it.
Add another line as:
test.php is the page where you want to post this form
<input type="button" border="0" value="Pay with Stripe" onclick="submitForm('test.php')">
in test.php you can get the values like this:
<input type="hidden" name="test" value="<?php echo $_REQUEST['test'] ?>">

Performs form action even out of the form

Hi having a problem with this code even if i clicked on cancel it will still proceed to use the action of my form even though the CANCEL button is not part of the form, Would appreciate any help.
Here is the code.
<form method="post" action="input_enroll.php" >
<div id="overlay1">
<div>
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" >
<input type="submit" value="Proceed To Enroll" '>
</form>
<input type="submit" value="Cancel" onclick='overlay()'>
</div>
</div>
EDIT: Any suggestions what would be a better idea? I'm thinking putting the cancel outside the div.
You are having form started, then divs started, then form closed..start form after divs..
as your markup is not correct, browsers will change it as thier parser suggest,
In chrome </form> tag is postponed after </div>s..
<div id="overlay1">
<div>
<form method="post" action="input_enroll.php" >
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" />
<input type="submit" value="Proceed To Enroll" />
</form>
<input type="submit" value="Cancel" onclick='overlay()' />
</div>
</div>

Categories