• Hello. I have a PHP code inside a Code Snippet, and I need to send data from a HTML page via the POST method, but I’m not sure how to do that. Any ideas?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hello,

    Do you already have a HTML form in a page? If so, all you need to do is intercept the POST request, like this:

    add_action( 'template_redirect', function () { 
    
    	if ( ! isset( $_POST['my_field'] ) ) {
    		return;
    	}
    
    	$value = $_POST['my_field'];
    
    	// process submitted data here
    
    } );
    Thread Starter phantomb

    (@phantomb)

    Hello, thanks for the response!

    This is my HTML form:

    <form action=”???” method=”post”>
    <input checked=”checked” name=”dye” type=”radio” value=”0″ /> Dye 1
    <input name=”dye” type=”radio” value=”1″ />Dye 2
    </form>

    I’m not sure what to put in the “action” field.

    Plugin Author Shea Bunge

    (@bungeshea)

    Where do you have this form on your website? Is it in a page or a post or something?

    Thread Starter phantomb

    (@phantomb)

    I have it in a page.

    Plugin Author Shea Bunge

    (@bungeshea)

    In that case, I would recommend something similar to this:

    add_action( 'template_redirect', function () { 
    
    	if ( ! is_page( 42 ) || ! isset( $_POST['dye'] ) ) {
    		return;
    	}
    
    	$dye = $_POST['dye'];
    
    	// process submitted data here
    
    } );

    You should replace 42 above with the ID, slug or name of your page. You don’t need to fill in the action field on the form; the data will just be submitted to the current page.

    When I try the code below I get:
    “Undefined index: dye in /opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/code-snippets/php/snippet-ops.php(352) : eval()’d code on line 2″

    <form action=”” method=”post”>
    <input checked=”checked” name=”dye” type=”radio” value=”0″ /> Dye 1
    <input name=”dye” type=”radio” value=”1″ />Dye 2
    </form>

    add_action( ‘template_redirect’, function () {
    $dye = $_POST[‘dye’];
    print_r($dye);
    } );

    Plugin Author Shea Bunge

    (@bungeshea)

    The check on lines 3-5 of the code snippet I posted is important – you are receiving this error because you removed it.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘How to sent POST information to a .php code inside a Code Snippet’ is closed to new replies.