prkj Posted September 9, 2013 Share Posted September 9, 2013 (edited) 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 September 9, 2013 by prkj Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/ Share on other sites More sharing options...
gristoi Posted September 9, 2013 Share Posted September 9, 2013 you cannot post to two different url's in one go. If you need to shoot some information off to 2 scripts sequentially then you can do it using ajax. On click fire off first ajax call, in the callback response you could then fire the second request. Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/#findComment-1448850 Share on other sites More sharing options...
prkj Posted September 9, 2013 Author Share Posted September 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/#findComment-1448851 Share on other sites More sharing options...
prkj Posted September 9, 2013 Author Share Posted September 9, 2013 "On click fire off first ajax call, in the callback response you could then fire the second request." I dont know how to accomplish this...please help Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/#findComment-1448852 Share on other sites More sharing options...
gristoi Posted September 9, 2013 Share Posted September 9, 2013 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 }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/#findComment-1448854 Share on other sites More sharing options...
prkj Posted September 9, 2013 Author Share Posted September 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/#findComment-1448864 Share on other sites More sharing options...
cyberRobot Posted September 10, 2013 Share Posted September 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/#findComment-1448964 Share on other sites More sharing options...
priyankagound Posted September 17, 2013 Share Posted September 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282011-two-actions-for-a-submit-button/#findComment-1449825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.