• Resolved AnotherChance

    (@anotherchance)


    Good afternoon, I am trying to update my custom table via Ajax but I am getting no joy. I have checked the network responses and the .js file has a 304 status and my .php file has received a 200 status. This leaves me to believe that the code in the PHP File is not right. I canot understand why because when I submit manually without Ajax it updates the database. Please help.

    <?php
    	if (!empty($_POST)) {
    		global $wpdb;
    		$table = 'ac_purchases';
    		$mc_data = array(
    			'purchase_post_id' = $_POST['REFERENCE'],
    			'days_purchased' = $_POST['dext'],
    			'purchase_date' = $_POST['TRANSACTION_DATE'],
    			'purchase_amount' = $_POST['total_due'],
    			'purchase_status' = 'pending',
    			);
    			array('%s', '%s', '%s', '%s', '%s');
    
    			$wpdb -> insert($table, $mc_data);
    
    			$response = array('my_hidden_field' => 'this is now filled in');
    
    			echo json_encode($response);
    	}
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter AnotherChance

    (@anotherchance)

    Good afternoon, I would like to update thia question.
    Below is the code for my form.

    <?php
    /*
     * Template Name: Confirmation
     */
     ?>
     <!DOCTYPE html>
    	<html>
    		<head>
    		<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    		<script src="<?php bloginfo('stylesheet_directory'); ?>/tset.js"></script>
    		</head>
    		<body>
    			<form method="get" name="testForm" id="testForm" action="https://www.paygate.co.za/paywebv2/process.trans">
    				<table>
    					<tr>
    						<td><label for="firstName">Name</label></td>
    						<td><input type="text" id="firstName" name="firstName" /></td>
    					</tr>
    					<tr>
    						<td><label for="lastName">Surname</label></td>
    						<td><input type="text" id="lastName" name="lastName" /></td>
    					</tr>
    					<tr>
    						<td><label for="age">Age</label>
    						<td><input type="number" id="age" name="age" min="0" max="100" /></td>
    					</tr>
    					<tr>
    						<td><input type="hidden" name="myHiddenField" id="myHiddenField" value="this is hidden" /></td>
    					</tr>
    					<tr>
    						<td><button name="submit" id="submit">Update</button><td>
    					</tr>
    				</table>
    			</form>
    		</body>
    	</html>

    here is the code for the php.

    global $wpdb;
    
    		$table = "tset";
    		$data = array(
    			"First_Name" => $_POST["firstName"],
    			"Last_Name" => $_POST["lastName"],
    			"Age" => $_POST["age"],
    			);
    			array(
    			"%s",
    			"%s",
    			"%d",
    			);
    		$wpdb -> insert($table, $data);
    
    		$response = array("myHiddenField" => "this is now filled in");
    
    		echo json_encode($response);

    and here is the script.

    $j = jQuery.noConflict();
    
    $j(document).ready(function(){
    	$j('#testForm').submit(submit_testForm);
    });
    
    function submit_testForm() {
    		$j.ajax({
    			url: "http://localhost/wordpress/wp-content/themes/responsive-mobile-child/tset.php",
    			type: "POST",
    			data: console.log($j('#testForm').serialize()),
    			dataType: 'json',
    			success: testForm_success
    		});
    		return false;
    	}
    
    function testForm_success() {
    	$j("#myHiddenField").val(response.my_hidden_field);
    	document.forms["testForm"].submit();

    Thread Starter AnotherChance

    (@anotherchance)

    The problem I am having is that the data received by Ajax appears not to be supported by the handler. For instance the data received by Ajax is

    firstName=NAME&lastName=SURNAME&age=42&myHiddenField=this+is+hidden

    This format appears to be incorrect for this request as I tested it at jsonlint.com and when validated it came up with the following.

    Error: Parse error on line 1

    firstName = NAME &

    ^
    Expecting ‘STRING’, ‘NUMBER’, ‘NULL’, ‘TRUE’, ‘FALSE’, ‘{‘, ‘[‘, got ‘undefined’

    Please help.

    Thread Starter AnotherChance

    (@anotherchance)

    Please close this topic, I’m all over the place here. I am going to open a new one.

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

The topic ‘Ajax Request’ is closed to new replies.