0

I have the following script:

if( condition ){
  echo '<form action="url" name="myForm" method="post">';
    echo '<input type="hidden" name="val" value="yes">';
    echo '<input type="submit" name="Ok" value="Ok">';
  echo '</form>';
}

When the condition is met,the submit button will show up,and i want it to be clicked with javascript,so the form will be submitted and i can use the hidden post value on the page from the action url."

I tried to add an id for the submit button and then use click(); but is not working.I also tried to create a function and use this.form.submit but it doesnt work because the action take place before the hidden value can be send.Any ideas?

7
  • 1
    Why are you bothering to go back to the browser at all? It would be simpler to just include a script with the functions you need and then call them directly. Commented Feb 3, 2015 at 8:37
  • What? I just need to use the hidden value on another page,but in order to send the value,i need a submit button(or atleast the form to be submitted).And i need the button to be auto clicked(when it shows up) , so it gives the impression that there is no form. Commented Feb 3, 2015 at 8:41
  • What is simpler? "Take data you have. Run code with it" or "Send data you have to the browser. Ask the browser to send the data back to the server. Run code with it". Commented Feb 3, 2015 at 8:43
  • The second one.But that can be accomplished with ajax, and i already tried that one but is not good for what i need! Commented Feb 3, 2015 at 8:44
  • why dont you just send a post request to other page when condition is true see lornajane.net/posts/2010/… Commented Feb 3, 2015 at 8:46

2 Answers 2

2

So the problem is that on submitting you are not getting hidden fields

Check the fiddle .I have just changed post request to get in order to see if parameters are passed and you can change it post later

Set form id to myForm and button id to ok

$(document).ready(function () {
    if ($("#myForm").length) {
      $("#ok").trigger('click');
        var url = window.location.href; 
        console.log(url);
    }

});

what I am doing is printing the url on console .So go to you browser console to see the url and I am getting the url with parameters url?val=yes&Ok=Ok

Sign up to request clarification or add additional context in comments.

2 Comments

one more thing , $("#myForm").length checks if the form have content ? because as much as i know .length is used to return the length of the string,and in the form there are no strings,just one button
$("#myForm").length return 1 when that id is in the html page and return 0 if id not in page .So if your php if condition is true then the form with that id is placed in the html page which returns 1( as jquery will only search in html page) and if php condition is false it will not place the form in html page returning 0.Try removing id of form in fiddle and check it would have same effect if your php condition is false
1

Try this it may work.

First add a id in your submit button

echo '<input type="submit" id="submit" name="Ok" value="Ok">';

Then you can use trigger in here like bellow code

$('#submit').trigger(submit);

Copy this bellow code this is working for me

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>


<?php 

$con=4;
if( $con==4 ){
  echo '<form  name="myForm">';
    echo '<input class="a" type="hidden" name="val" value="yes">';
    echo '<input id="submit" type="submit" name="Ok" value="Ok">';
  echo '</form>';
  echo "<script>
  jQuery('form').submit(function(e){
         e.preventDefault();
         var data=$('.a').val();

         $.post(
            'page2.php',
            {value:data},
            function(data){
                alert(data);
            }
         );

     });
         jQuery('form').trigger('submit');


</script>
         ";
}

?>

2 Comments

If your condition will true then submit button will click automatically then your hidden input field value will send another page,do you want this ?
Yes,that's exactly what i'm looking for!

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.