Support » Plugins and Hacks » Hacks » Save data to Mysql from page template form

Save data to Mysql from page template form

  • Hello to all,
    I’m trying to save the form data from one page to wordpress Mysql database in ajax.

    The script works but the data is not washed out, if anyone can help me thanks!
    save_data.php content:

    <script type="text/javascript">
    		var name = jQuery("#mc-name").val();
    	    var name = jQuery("#mc-email").val();
    	    var name = jQuery("#mc-phone").val();
    	    var name = jQuery("#result_ricavi_vendita").val();
    	    var name = jQuery("#result_materie_prime").val();
    	    var name = jQuery("#result_utilities").val();
    	    var name = jQuery("#result_consumabili").val();
    	    var name = jQuery("#result_costi_variabili").val();
    	    var name = jQuery("#result_personale").val();
    	    var name = jQuery("#result_affitto").val();
    	    var name = jQuery("#result_ammortamento").val();
    	    var name = jQuery("#esult_manutenzione").val();
    	    var name = jQuery("#result_varie").val();
    	    var name = jQuery("#result_costi_fissi").val();
    	    var name = jQuery("#result_tot_costi").val();
    	    var name = jQuery("#result_ebit").val();
    	    var name = jQuery("#result_ebit_perc").val();
    	    var name = jQuery("#last_message").val();
    		type: 'POST',
    		url: ajaxurl,
    		data: {"action": "esmach_app", "mc-name":name},
    		success: function(data){
    		alert('Prospetto salvato con successo!');

    jQuery script

    	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    	function esmach_app(){
    $name = $_POST['mc-name'];
    			$email = $_POST['mc-email'];
    			$phone = $_POST['mc-phone'];
    			$result_ricavi_vendita = $_POST['result_ricavi_vendita'];
    			$result_materie_prime = $_POST['result_materie_prime'];
    			$result_utilities = $_POST['result_utilities'];
    			$result_consumabili = $_POST['result_consumabili'];
    			$result_costi_variabili = $_POST['result_costi_variabili'];
    			$result_personale = $_POST['result_personale'];
    			$result_affitto = $_POST['result_affitto'];
    			$result_ammortamento = $_POST['result_ammortamento'];
    			$result_manutenzione = $_POST['result_manutenzione'];
    			$result_varie = $_POST['result_varie'];
    			$result_costi_fissi = $_POST['result_costi_fissi'];
    			$result_tot_costi = $_POST['result_tot_costi'];
    			$result_ebit = $_POST['result_ebit'];
    			$result_ebit_perc = $_POST['result_ebit_perc'];
    			$last_message = $_POST['last_message'];
    global $wpdb;
     array( 'mc-name' => $name, 'mc-email' => $email, 'mc-phone' => $phone, 'result_ricavi_vendita' => $result_ricavi_vendita, 'result_materie_prime' => $result_materie_prime, 'result_utilities' => $result_utilities, 'result_consumabili' => $result_consumabili, 'result_costi_variabili' => $result_costi_variabili, 'result_personale' => $result_personale, 'result_affitto' => $result_affitto, 'result_ammortamento' => $result_ammortamento, 'result_manutenzione' => $result_manutenzione, 'result_varie' => $result_varie, 'result_costi_fissi' => $result_costi_fissi, 'result_tot_costi' => $result_tot_costi, 'result_ebit' => $result_ebit, 'result_ebit_perc' => $result_ebit_perc, 'last_message' => $last_message, ),
    	) ); 
    return true;

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

    I have already created the table manually wp_ajax_demo but the data is not saved.
    thank you

Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    Your PHP function needs to be hooked into the ‘wp_ajax_esmach_app’ action. The form user must be logged in for this to work, which appears to be your intention.

    Your jQuery script is not building a name array, it is overwriting the same variable with a new value each time. Use the push() method to build an array. There is no need to push individual values anyway. You can serialize all the form values in a single line of code.

    When PHP receives the serialized data, it can be directly stored in the table as a string. Any array in PHP would need to be serialized before storing in a table anyway. This is typically done automatically, but the data being serialized to begin with saves PHP the trouble. However, whatever code uses the stored data may need to unserialize the string in order to access the data.

Viewing 1 replies (of 1 total)
  • The topic ‘Save data to Mysql from page template form’ is closed to new replies.
Skip to toolbar