• jegan99

    (@jegan99)


    I have created a custom plugin in php where I included my HTML page and I want to invoke a function in this page on submit of HTML form.
    Note that I have included my server.php (custom plugin) in same folder of my html page.
    invoke a function in this page on submit of form

    This is my HTML form

    #######################################################################################

    <?php get_header(); ?>
    
    	<div id="content">
    		<form method="post" action="">
                    <label for="firstName"> First Name </label>
        <input id="firstName" name="firstName" type="text" required/>
    
        <label for="lastName">Last Name </label>
        <input id="lastName" name="lastName" type="text" required/>
    
        <label for="birthDate">Birth Date </label>
        <input id="birthDate" max="2018-12-31" min="1900-01-01" name="birthDate" type="date" value="2018-01-01" />
    
        <label for="employeeID"> Employee ID </label>
        <input id="employeeID" name="employeeID" type="number" required/>
    
        <label for="companyEIN"> Company EIN </label>
        <input id="companyEIN" name="companyEIN" type="number" />
    
        <label for="fileToUpload"> Upload supporting documents </label>
        <input id="fileToUpload" name="fileToUpload" type="file" multiple>
        <input id="submit" type="submit">
    
    					</form>
    	</div><!-- /#content -->
    
    <?php get_footer(); ?>

    #######################################################################################
    This is my PHP plugin in same folder

    <?php
    /*Plugin Name:Sample server
      Description:Trying Sample server.php
      version: 1.0
      Author :Jegan 
    */
    function html_form_code()
    {
    include('serverform.html');
    }
    function form_print()
    {
    echo "Invoked";
    }
    add_action( 'init', 'form_print' );
    add_shortcode('sampleServer','custom_form_shortcode');

    I need to invoke this form_print only onsubmit of HTML page and I need to send some message to HTML form. what should be my add_action in this case.
    Looking forward for your reply.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter jegan99

    (@jegan99)

    Can you please tell me what is the wrong?
    I have created a plugin to include my external HTML code. Is it possible to invoke the php function in my plugin on submit?

    Moderator bcworkz

    (@bcworkz)

    There are a few things wrong. One is you HTML page contains PHP, its file extension needs to be .php or the server will not process the PHP code.

    Two is you must not generate output from the “init” action. It will result in invalid HTML. It will work, but the page could never validate as W3C compliant.

    It’s not clear how your form is initially output. I’m assuming via a shortcode in page content, which is fine, but then you don’t want to call get_header() and get_footer(). Keep the .php extension though, you will likely still want to add PHP to the form, such as to conditionally output a message.

    Additionally, shortcode handlers must not generate output as well. Instead, all shortcode content needs to be returned from the handler as a single string. If you have a lot of HTML output that needs to be handled as a shortcode, you can use the PHP output buffer to capture HTML output, then return the buffer contents.

    There are a number of ways to have your PHP code determine if the current request is the initial one or as a result of form submittal. One is to check the HTTP method in the $_SERVER array. If it’s “POST”, it’s due to the form submittal. Or check for the presence of a required field in $_POST. Then you can add something like this to your form:

    <?php
    if ( array_key_exists('employeeID', $_POST)) { echo 'Thank you for submitting!';}
    ?>

    Use similar logic to process the submitted form data. This can be done as part of the shortcode handler as long as your form submit just reloads the same page. This will be preferable to using the “init” action, even if no output results. Try to only use “init” for code required to run on every request.

    I see you are new to the forums. Welcome!
    There are a couple things you ought to know about these forums. When you post code, please demarcate with backticks or use the code button. If you don’t, the forum’s parser will corrupt your code. A moderator fixed your OP code for you, it’s best if you do so yourself.

    You should try to avoid adding another post to your topic before anyone replies. Doing so causes your topic to fall off of the “no replies” list that many regulars use to find those still needing help. You can edit your topic for 30 minutes if you think of other information you need to add. Some people will try to bump their topic up by posting again. Not only is this against our guidelines and causes your topic to actually be less visible, it does not even move your topic up in the main forum listing like bumping does in other forums.

    Thread Starter jegan99

    (@jegan99)

    Still Iam not getting the result. Is it possible to connect with each other through skype, so that I can explain what I trying to do. Actually my question is what should be the

    $tag

    in add_action and few questions I have. So, can we connect through skype. Looking forward for your reply.

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, support outside of official channels is strongly discouraged, so Skype is not an option. I know the forum format can be frustrating. If you prefer, support via IRC chat is available.

    Thread Starter jegan99

    (@jegan99)

    Sure what is IRC chat support. can you explain please?

    Moderator bcworkz

    (@bcworkz)

    Imagine SMS texting with a small to large group of people together at the same time. Except you use a web service or client app instead of a phone’s SMS function. Another description here. It’s ancient Internet technology that has largely been replaced by social media, but remains relevant in a few instances. It’s worth checking out just for the historical experience ??

    Much like these forums, knowledgeable volunteers willing to help out hang out there, except the turn around is in real time, or at least as long as it takes to type a reply. But if there is not ongoing activity, it can take a while before someone notices a new question.

    Information specific to the #wordpress channel is linked in my previous post. How many people are on IRC #wordpress at any given time varies a lot. You can expect the largest group during weekdays/daylight hours of the western hemisphere, but there’s usually someone online most of the time. All it takes is one person who is willing to help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to call a Plugin made by myself on submit of HTML?’ is closed to new replies.