Jump to content

Two actions for a submit button


prkj

Recommended Posts

Hello all,

 

I am trying to call two functions onclick of a submit button. the two functions inturn go to two php scripts..i want the php scripts to be run one after the other.

But, its not working. Please take a look at my code and suggest some changes

my html :

<form method="post" action="" name="form">
<label> New Data set: </label>
<input type="radio" name="url" value="NetOptInput2.html" id="ex1" required/> Yes
<input type="radio" name="url" value="NetOptResult2.html" id="ex2" required/> No
<br><br><br>
<label>Dataset description:
</label>
<input type="text" name="dataset" id="field1" size="30" placeholder="" readonly><br><br><br>
<label>Token Number : </label><input type="text" name="token" id="field2"   size="6" placeholder="" readonly><br><br><br>
 
<div style="text-align: center"><br>
<input type="Submit" name="submit" value="Submit" class="submit" onclick="return(OnButton2() && OnButton1())">
<div class="spacer"></div> 
</form> 

My Javascript

 <!-- this function navigates user according to the radio button selection-->
<script language="Javascript">
function OnButton1()
{
    document.form.action = "Input1a.php"
    // document.form.target = "_blank";    // Open in a new window
 
    //document.form.submit();             // Submit the page
 
   // return true;
}
 
function OnButton2()
{
    document.form.action = "Input1b.php"
     
    //document.form.target="_blank";
  // document.form.submit();             // Submit the page
 
   // return true;
}
 
<!--This function disables the Token Number form if the user clicks "yes" radio button and disables Dataset if "No"-->
<script type="text/javascript">
$(function(){
$("#ex1, #ex2").change(function(){
    $("#field1, #field2").val("").attr("readonly",true);
    if($("#ex1").is(":checked")){
        $("#field1").removeAttr("readonly");
        $("#field1").focus();
    }
    else if($("#ex2").is(":checked")){
        $("#field2").removeAttr("readonly");
        $("#field2").focus();   
    }
});
});
 

MY php file1

if (isset($_POST) && $_POST['url1'] == "Yes")
    {
        header('Location: NetOptInput2.html');
    }
    else if (isset($_POST) && $_POST['url2'] == "No")
    {
        header('Location: NetOptResult2.html');
    }
my php file 2

if (isset($_POST['submit'])) { 
$dataset = $_POST['dataset'];
$tokennumber = $_POST['tokennumber'];
$data = "$dataset | $tokennumber\n";
$file = "input.txt"; 
 
$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!"); 
 
fclose($fp); 
$message = "Saved to $file successfully!";
}
Edited by prkj
Link to comment
Share on other sites

i tried using ajax  for one of those functions. i used the below code,, instead of my php file 1

 

<script type="text/javascript">
$(function(){

$('form').submit(function(event){
event.preventDefault();
window.location = $('input[type=radio]:checked').val();
});
});
</script>
 

but, if i do event.preventDefault() the form is not going to my php..

I need to do these two tasks by hitting the submit button

1) change the url according to the radio button selection

2) run the php script to save the form data

Please help me with this..

Thanks

Link to comment
Share on other sites

you need to read up a little on your javascript. you used JQUERY not AJAX.

$('form').submit(function(event){
event.preventDefault();
var checkedVal = $('input[type=radio]:checked').val();
$.ajax({
  type: "POST",
  url: 'myscript1.php',
  data: checkedVal,
  success: function(){ // make second callback here},
  dataType: dataType
});
});
});
Link to comment
Share on other sites

Sorry about that.. i know that it is JQuery but, thinking about AJAX call i typed it. I am a newbie to web development(AJAX too)

i am going step by step. I tried to write the call back inside success function, but, couldnt make it work..I havent written any AJAX before..could you please help me writing the Call back in the success function.Thanks alot for your help

Link to comment
Share on other sites

Could you provide a little more information as to why you're trying to call two different scripts?

 

If there isn't a way to merge those scripts, have you considered just calling the first script. Once that script successfully executes, use the header() function to redirect them to the second script. Of course, you would need to pass the form data with the redirect.

Link to comment
Share on other sites

Once a form is submitted, you are no longer on that page. You've navigated away.


The other way you can do this is submit the first action via AJAX, then submit the form naturally to the second destination. I would suggest using jQuery to make your AJAX calls since most of the AJAX code is already there for you to use.


Another option is to have raf-submitted.php perform a POST from your server to the salesforce server once it receives the form data.


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.