0

I want to show selected data after click on submit.I have search too much but not get proper answer.

enter image description here

i used this code but what will be the next step i am not getting this.

if($actiondone!=''){
    echo "<script>
        $('#actiondoc').val();
        </script>";
}

Please guide me for solutuion

Thanks

Bijender Singh Shekhawat
  • 3,934
  • 2
  • 30
  • 36

1 Answers1

0

You cannot use PHP to interact with the user.

After the page has rendered, PHP is finished. If you want to interact with the user (i.e. detect changes to drop-downs, etc) you must use jQuery/javascript only.

For your example, you need to have the jQuery code pre-existing in the page - that is, the javascript gets written into the DOM as the page is being initially rendered. You can add the jQuery/javascript code as part of the HTML, or you can echo that code in PHP as the page is being built -- but it must be on the page when the user gets control of the DOM.

Working demo:

$(function(){
    $('#form_submit').click(function(){
        alert( $('#actiondoc').val() );
        return false; //this line cancels the SUBMIT action of the form
    });
    
    $('#actiondoc').change(function(){
      alert('You chose: ' + $(this).find('option:selected').text() );
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<form id="your_form">
    <select id="actiondoc">
        <option value="">Choose:</option>
        <option value="chg">change of tyre</option>
        <option value="rnw">renew tyre</option>
    </select>
    <input id="form_submit" type="submit" value="submit" />
</form>

You say that you want to show the selected choice after user clicks on submit, but there might be a problem with that idea:

When user clicks submit, an HTML form will immediately (a) get the values of the form controls and (b) deliver them to the page specified on the <form action="new_page.php" method="post"> -- so, in this code right here, the browser will navigate over to new_page.php and deliver the form values to that page.

The point is: the page will change (unless you are using AJAX - are you?) - so you cannot show anything more on that page because the user isn't on that page any longer.

So, you can show the value of the #actiondoc control on the new page, OR you can intercept the submit function, as I did in my demo above when I displayed the alert()

If you tell us more about exactly what you want to do, we can help more.


References:

https://www.w3schools.com/xml/ajax_intro.asp

AJAX vs Form Submission

YouTube - Simple AJAX Contact Form - HTML, PHP, AJAX

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • when i remove return false its not working fine ,i need submit buttton should work – warisha iqbal Feb 18 '19 at 17:57
  • i am not using ajax – warisha iqbal Feb 18 '19 at 18:06
  • In that case, the page will switch away from the webpage that the `#actiondoc` dropdown is on, over to the page specified in your `
    ` tag. Please: (1) post your HTML that includes the form tag, and (2) tell us what you want to happen when the user clicks the `submit` button. Please tell us *exactly* (everything) you want to happen, step-by-step. *Thanks!*
    – cssyphus Feb 18 '19 at 18:33
  • Note: ***If*** your form tag does not specify another page in the `action=""` attribute on the form tag, then the page will simply reload. For example: If your form tag just looks like this: `
    `, then when the user clicks the `submit` button inside that form, the page will just reload itself. If that is happening, now you know why.
    – cssyphus Feb 18 '19 at 18:35