• I am trying to process a form, but I do not know where to put the php code to process it.
    Should I use a php file and put it on action attribute of form?
    Should i use some hook?
    Should I use javascript to call php?

    The form will be placed in a page with a shortdcode (this part is solved),
    My form should do:
    – collect user data
    – check user data (javascript)
    – process payment (in another web)
    – receive information of payment
    – update user database if payment is OK
    – send email to user and admin

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator bcworkz

    (@bcworkz)

    Any one of those approaches could work. The last two exactly describe a WP style AJAX request. The first is perhaps the most straight forward. I would implement it as a plugin, though a child theme extension would probably work.

    Whatever file that is named as the form action will need to require_once the wp-admin/admin.php file to gain access to WP resources and ensure the user is logged in. For security reasons, be sure you implement a nonce check, check user capability, verify and sanitize form values (even though done client side as well), etc.

    Thread Starter capbussat

    (@capbussat)

    I used an strategy of using file template TemplateMyForm.php, put it in theme directory which shows the form. The from a PAGE I change the template and form is shown, but still does not process the form.
    Here is the code:

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. Closing with the single quote character doesn’t work. ]

    <?php
    /*
    Template Name: MyForm
    */
    ?>
    
    <?php get_header(); ?>
    <?PHP
    	if ( isset($_POST['itm_paypal_hidden']) ){  // && 'y' == $_POST['itm_paypal_hidden']
     		// Save the new settings in the variables.  These will be displayed in the form.
    			$name = $_POST["name"];
    			$lastname = $_POST["lastname"];
    			$email = $_POST["email"];
    		// Put the new settings in an array.
    			$options = array(
    			'email' => $email,
    			'name' => $name,
    			'lastname' => $lastname,
    			);
    
    		// Save the array of new values in the WP database.
    		update_option( 'itm_paypal_options', $options );
    
    	}  else {
    		// $itm_url = "127.0.0.1:4001/wordpress/wp-content/plugins/process_form.php";
    		// Get the current settings from the database.
    		$options = get_option( 'itm_paypal_options');
    		// Populate these variables with the current settings.  These will then be displayed in the form.
    		$email = $options['email'];
    		$name = $options['name'];
    		$lastname = $options['lastname'];
    	} 
    
    ?>
    
    <div id="content-full" > <!-- class="grid col-940" -->
    
    		<div class='contenidor' width = 300px>
    			<p><form method="POST" enctype="text/plain" action="">  <!-- ../wp-content/plugins/process_form.php -->
    			<input type="hidden" name="itm_paypal_hidden" value="y" >
    			<label for="name">Nombre:</label>
    			<input name="name" type="text" value="" width=300px placeholder="Nombre"></p>
    			<p><label for="last_name">Apellido:</label>
    			<input type="text" name="lastname" placeholder="Apellido" width=300px /></p>
    			<p><label for="email" type="email" >email:</label>
    			<input name="email" type="text" value="<?php echo $email?>"  width=300px ></p>  <!-- placeholder="[email protected]" -->
    			<button name="submit" type="submit" value=""> Envia</button>
    			</form>
    		</div>
    
    </div>
    
    <?php get_footer(); ?>

    Ya.,
    Its not working. because form does not submit in this template so you can use or create a plugin

    Please refer following code for code

    Template

    <div class="widget newsletter">
        <h3>News Letter</h3>
        <form action="#">
            <input type="text" class="newsletter-user" placeholder="Enter Email ID" class="required"  />
            <input type="hidden" class="newsletter-url" value="<?php bloginfo('template_url');?>/ajax.php" />
            <input type="button" name="submit" onclick="addSubscriber()" value="" />
        </form>
    </div>

    ajax.php

    <?php
    include '../../../wp-load.php';
    if( $_POST['type'] == 'newsletter' ){
    	$user_id = username_exists( $_POST['user'] );
    	if( !$user_id and email_exists( $_POST['user'] ) == false &&  is_email($_POST['user']) ) {
    		$password = wp_generate_password( $length=12, $include_standard_special_chars=false );
    		wp_create_user( $_POST['user'] , $password , $_POST['user'] );
    		echo '
    		<h3>Newsletters Sign Up</h3>
    		<p>Thankyou...</p>
    		';
    	}
    	else{
    		echo '
    		<h3>News Letter</h3>
    		<p style="border:1px solid #ff0000; padding:2px; font-color:#ff0000">Email already exist / Enter valid email</p>
    		<form action="#">
    			<input type="text" class="newsletter-user" placeholder="Enter Email ID" class="required"  />
    			<input type="hidden" class="newsletter-url" value="'.get_bloginfo('template_url').'/ajax.php" />
    			<input type="button" name="submit" onclick="addSubscriber()" value="" />
    		</form>
    		';
    	}
    }?>

    jQuery

    function addSubscriber(){
    	var user = $('.newsletter-user').val();
    	var url = $('.newsletter-url').val();
    	var data = 'type=newsletter&user='+user;
    	$.ajax({
    		url		: url,
    		type	:'POST',
    		data	: data,
    		success	: function(val){
    			$('.newsletter').html(val);
    		}
    	});
    }

    Please analysis this code

    Feel free for discuss

    Thanks

    Thread Starter capbussat

    (@capbussat)

    Thanks, so much for the valuable information!
    I think I understand most of it. Using a template that contains the form, using javascript to call ajax.php (which repeats the form if user already exists) javascript is called to send the POST data , and needs jQuery

    Some questions:
    1) Javascript and access jQuery must be put in header by plugin. is it correct?
    2) Template php file with form in theme and ajax.php in same directory
    3) In template form: what does mean action=”#” ?

    I will try to correct my code for tomorrow
    Thaks again

    Moderator bcworkz

    (@bcworkz)

    1) Right. Use wp_enqueue_script() to do that. Specify jquery in an array as a dependency to your javascript and all the proper links will be placed in the head section.

    2) Template files go in the theme folder, other files can go anywhere as long as the paths resolve correctly. It’s common practice to put utility type php files in an “includes” subfolder and .js files in a “js” subfolder, but you can do what you want.

    3) The hash (#) is a sort of placeholder in this context. You will see it when an event (such as mouseclick) handles the form submit instead of the submit button. If it still is handled by the submit button, the form data will go to the same file on which the form resides. Such pages usually have some sort of logic like if request is GET, send the form. If request is POST, process the data.

    Hi capbussat.,
    action=”#” means form post/get nothing the form is dummy this form submit via ajax

    Thread Starter capbussat

    (@capbussat)

    I corrected some things, but I get a new error wich does not recognize some jQuery javascript from Firebug :
    TypeError: $ is not a function
    [Break On This Error]
    var email = $(‘.itm_email’).val();

    My code in a file script .js, after adapting to my form looks like this:

    function itm_paypal_ajax(){
    var email = $(‘.itm_email’).val();
    var name = $(‘.itm_name’).val();
    var lastname = $(‘.itm_lastname’).val();
    var url = $(‘.itm_url’).val();
    var data = ’email=’+email+’&name=’+name+’&lastname=’+lastname; // is it correct?
    $.ajax({
    url : url,
    type :’POST’,
    data : data,
    success : function(val){
    $(‘.paypal_ajax’).html(val); // returns where? what does this code?
    }
    });
    };

    I have tried also to embed this code inside
    jquery(document).ready(function(){
    // code here
    {);
    Then I get another error
    What is the correct script?

    Remove $ sign to replace JQuery

    ie : jQuery('.className').val()

    Try This

    Thread Starter capbussat

    (@capbussat)

    Thanks Rajan V, changing jQuery for $ worked and I made one correction by intuiton ( i do not understand some of the Javascript), now it seems Javascript works. I corrected also one address of ajax.php script. Firebug shows it works but I should I see any change on the screen? It does not seem that ajax.php shows anything on the screen, code below.

    Javascript:

    function itm_paypal_ajax(){
    	var email 	= jQuery('.itm_email').val();
    	var name 	= jQuery('.itm_name').val();
    	var lastname = jQuery('.itm_lastname').val();
    	var url = jQuery('.itm_url').val();
    	var data = 'email='+email+'&name='+name+'&lastname='+lastname; // is it correct?
    	jQuery.ajax({
    		url		: url,
    		type	:'POST',
    		data	: data,
    		success	: function(val){
    			jQuery('.itm_paypal_ajax').html(val);  // <=OK
    		}
    	});
    };
    
    ajax.php:
    <?php
    
    include '../../../wp-load.php';
    if( isset ($_POST['email'])){
    	if ( isset($_POST['email'] ) && is_email($_POST['email']) ) {
    		$name = $_POST["name"];
    		$lastname = $_POST["lastname"];
    		$email = $_POST["email"];
    		echo '
    		<h3>Formulario correcto</h3>
    		<p>Gracias </p>
    		'+$name+' '+$lastname+' '+$email+' ';
    	}
    	else{
    		 ?>
    			<h3> Formulario </h3>
    			<form action="#">
    			<label for="name">Nombre:</label>
    			<input  type="text" name="name" class="itm_name" value=""  placeholder="Nombre">
    			<label for="lastname">Apellido:</label>
    			<input type="text" name="lastname" class="itm_lastname"  placeholder="Apellido" />
    			<label for="email">Email:</label>
    			<input type="text" name="email" class="itm_email" placeholder="Enter Email ID" class="required"  />
    
    			<input type="hidden" class="itm_url" value="<?php get_bloginfo('template_url');?>/ajax.php" />
    			<input type="button" name="submit"  value="onclick="itm_paypal_ajax()"> 			
    
    			</form>
    		<?php
    	}
    }

    [Please post code between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Hi buddy.,

    Its Ajax it cant show on firebug. What is your actually need? please explain me i defiantly help you

    Rajan

    Thread Starter capbussat

    (@capbussat)

    Thanks Rajan V,
    I used an ajax.php file and I can see in Firebug that POST data is sent correctly from form with javascript to ajax.php file

    Then I should put code in the ajax.php file to process data from the form and for example add subscriber to database.
    1) I do not see that user is created. Maybe is not working.
    2) How can I change the page that is viewed in the browser?: if data is correct end process and thank user, if data is not correct return to previous form and ask user to enter data again.

    https://pastebin.com/jjVLk75Y

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Where to put PHP code to process a form in a page’ is closed to new replies.