1

i am using the following code to auto-submit a form

<script type="text/javascript">
$(function () {
    $('#submit').click();
});
</script>

<div style="display:none">
<form method="post" action="http://mydomain/texting/register.php?list_id=15">
<input type="hidden" name="name" value="<?php echo $_SESSION['name2']; ?>" />
<input type="hidden" name="email" value="<?php echo $_SESSION['email2']; ?>" />
<input type="hidden" name="ok" value="1" />
<input type="submit" id="submit" value="Sign Up" style="visibility:hidden;" />
</form>
</div>

i want to submit the same data to two urls ...

http://mydomain/texting/register.php?list_id=15
http://mydomain.com/texting/register.php?list_id=<?php echo $_SESSION['city2']; ?>

how can edit my code to submit same form data automatically?

i tried this code

<script type="text/javascript">
function submitTwice(f){
f.action = 'http://mydomain.com/texting/register.php?list_id=<?php echo $_SESSION['city2']; ?>';
f.target='ifr1';
f.submit();
f.action = 'http://mydomain.com/texting/register.php?list_id=15';
f.target='ifr2';
f.submit();
}
</script>
*/


<script type="text/javascript">
$(function () {
    $('submitTwice(f)').click();
});
</script>


<div style="display:none">
<iframe name="ifr1" width="20" height="20">
<form method="post" action="http://mydomain.com/texting/register.php?list_id=<?php echo $_SESSION['city2']; ?>">
<input type="hidden" name="name" value="<?php echo $_SESSION['name2']; ?>" />
<input type="hidden" name="email" value="<?php echo $_SESSION['email2']; ?>" />
<input type="hidden" name="ok" value="1" />
<input type="submit" id="submit" value="Sign Up" style="visibility:hidden;" />
</form></iframe>
</div>
<div style="display:none"><iframe name="ifr2" width="20" height="20">
<form method="post" action="http://mydomain.com/texting/register.php?list_id=15">
<input type="hidden" name="name" value="<?php echo $_SESSION['name2']; ?>" />
<input type="hidden" name="email" value="<?php echo $_SESSION['email2']; ?>" />
<input type="hidden" name="ok" value="1" />
<input type="submit" id="submit" value="Sign Up" style="visibility:hidden;" />
</form></iframe>
</div>

but it is not working, any suggestion to make it happen, the same data is to be sent to both the urls automatically using javascript

3
  • If the forms are on the same domain, you can put them in two `<iframe> elements and post them. Commented Feb 18, 2012 at 22:57
  • sending forms are on same domain only list_id differs Commented Feb 18, 2012 at 23:05
  • the code will reside on server Commented Feb 19, 2012 at 0:35

2 Answers 2

2

You can use jQuerys ajax and send the data with post, to both the urls.

first save all data you want to send in an valid data array. if you want it automated, you can do something like this:

var sendData = {};
$("#formId").find("input").each(function(i, item){
    sendData[item.name] = item.value;
});

or as a simple string:

var sendData = "name=value&email=value";

then send the form data to both urls with ajax, as post type:

$.ajax({ url: "http://url1/", type: "POST", data: sendData,
         success: function(response){
             alert(response);
         }
});
$.ajax({ url: "http://url2/", type: "POST", data: sendData,
         success: function(response){
             alert(response);
         }
});

Note: make sure to return false to the button or sending form, you can do that something like:

$("formId").submit(function(){
    // do ajax send data
    return false;
}

EDIT: added unchecked code for this, with comments

$(document).ready(function(){
    // grab the form and bind the submit event
    $("#formId").submit(function(){
        // collect all the data you want to post
        var sendData = {};
        this.find("input").each(function(i, item){
            sendData[item.name] = item.value;
        });

        // send the request to both urls with ajax
        $.ajax({ url: "url1", type: "POST", data: sendData,
                 success: function(response){
                    // handle response from url1
                    alert(response);
                 }
        });
        $.ajax({ url: "url2", type: "POST", data: sendData,
                 success: function(response){
                    // handle response from url2
                    alert(response);
                 }
        });

        // return false, to disable default submit event
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

i am quite a new-bee, please explain a bit more
go with one single form, and add an id tag to it, then check the code added in my post, hopes it get you started
1

just send name and email to the server script in a regular form, then do two requests from within your register.php with something like HttpRequest or the like...

some freehand code:

client:

<form id="autoSubmitForm" method="post" action="/register.php?list_id=15">
    <input type="hidden" name="name" value="<?php data['name2']; ?>" />
    <input type="hidden" name="email" value="<?php data['email2']; ?>" />
    <input type="submit" name="submit" value="Sign Up" style="visibility:hidden;" />
</form>
<script>
    $(document).ready(function(){
       $('#autoSubmitForm').submit();
    });
</script>

server:

<?php
   $name = $_POST["name2"];
   $email = $_POST["email2"];
   $url1 = 'http://lala'; //or relative to server root: '/lala'
   $url2 = 'http://other';
   sendRequest($url1, $name, $email);
   sendRequest($url2, $name, $email);
?>

The implementation of sendRequest is worth another question (or reading the documentation of HttpRequest mentioned above)

Good luck, I see your duplicate question got closed...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.