I admit I'm over my head on this. I'm running out of time as I need this page to work by Monday and I'm getting desperate; I've spend the last week googling and combing through stackoverflow answers and I still can't figure out how to get this to work.
I have 1 form with two submit buttons at the bottom, HOLD RESERVATION and BOOK NOW. Each button needs to submit the form data (just in an email, nothing fancy), then redirect either to a reservation confirmation static html page, or to my booking page.
I've tried this, which redirects nicely, but doesn't email me the form data:
<input type="submit" onclick="var e = document.getElementById('mpNOV'); e.submit(); e.action='mp-NOVconfirm.shtml';" value="Hold my reservation" class="submit"></td>
<td width="45%" style="padding-top:10px; padding-bottom:10px; border-top: 1px solid #6f3957;">
<input type="submit" onclick="var e = document.getElementById('mpNOV'); e.submit(); e.action='../../index.shtml';" value="Book Now!" class="submit">
I've tried having my FORM ACTION="external.php" with this being the php code (based on php code I used for a different form submit):
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(isset($_POST['Reservation']))
{
$subject = 'New Machu Picchu RESERVATION - November';
$emailadd = 'jodi@happywivestravel.com';
$url = 'http://www.happywivestravel.com/tour/machupicchu/mp-NOVconfirm.shtml';
$req = '0';
$text = "Results from form:
";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('
', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
if(isset($_POST['Book']))
{
$subject = 'New Machu Picchu BOOKING - November';
$emailadd = 'jodi@happywivestravel.com';
$url = 'http://www.happywivestravel.com/';
$req = '0';
$text = "Results from form:
";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('
', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
}
?>
But there must be something I did wrong here, because nothing happens when I click the submit button(s). No redirect, no form data emailed.
I originally wanted to get this function to work with a captcha script, but I've had to scrap that for now. I just need to get these submit buttons working properly asap.
I know next to nothing about php, and I'm very green with javascript (I'm learning as I go here). I really would appreciate any help here. THANK YOU!!
Why don't you but the buttons in a form tag? And navigate with that?
<form id="formid" action="link" method="post">
This is where jQuery is your bestfriend!
Two bottons with IDs.
<input type="button" id="btn1" value="Hold my reservation" class="submit">
<input type="button" id="btn2" value="Book Now!" class="submit">
And then you'll take jQuery to detect two buttons click actions by ID and within that and send an Ajax call as a POST request (form submit) and then redirect! BOUM.
$('#btn1').click(function()
{
$.ajax(
{
url: "submit_script.php",
type: "POST",
data: {input1: input1, input2: input2},
dataType: "html",
success: function(data)
{
window.location = 'http://www.example.com/page.php';
}
});
});
$('#btn2').click(function()
{
$.ajax(
{
url: "submit_script.php",
type: "POST",
data: {input1: input1, input2: input2},
dataType: "html",
success: function(data)
{
window.location = 'http://www.example.com/page.php';
}
});
});
You can get creative within the Ajax and send whatever input fields you desire for each. This is how you will separate them, and/or send each click to a different submitting URL.
You can also pass data to "page.php" by using the data variable within the function parameter.
OR
You can detect which button you press within the submit form simply by renaming the button name and then detect it within the post on the submission page.
Reference this page for the above solution:
stackoverflow.com/questions/547821/two-submit-buttons-in-one-form
Try naming your submit buttons Reservation
and Book
, like this:
<input type="submit" onclick="this.name='Reservation'; var e = document.getElementById('mpNOV'); e.submit(); e.action='mp-NOVconfirm.shtml';" value="Hold my reservation" class="submit"></td>
<td width="45%" style="padding-top:10px; padding-bottom:10px; border-top: 1px solid #6f3957;">
<input type="submit" onclick="this.name='Book'; var e = document.getElementById('mpNOV'); e.submit(); e.action='../../index.shtml';" value="Book Now!" class="submit">
An easier jquery method:
$('#buttonid1').click(function() {
$('#formid').attr('action', '/path/page1/');
$('#formid').submit();
});
$('#buttonid2').click(function() {
$('#formid').attr('action', '/path/page2/');
$('#formid').submit();
});
just sayin...