dimanche 26 juin 2016

PHP form with jquery and ajax in different documents

How can I submit a form in a PHP document, take that values with a Jquery that is in another document and send it by Ajax to another PHP document.

Note: If there is a way to put the Ajax into the HEAD tag, I'll apreciate if you tell me how so I would discart the option with Jquery. But we really want to put the less as possible code in the Item.php file

Thank you very much !

Here is what we got:

// Item.PHP 

<head>
    <meta http-equiv="content-type" content="text/html; UTF-8" />
    <link href="../css/home.css" rel="stylesheet" type="text/css">
    <title>Items</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
    <script src="../includes/ec.js"></script>
    <script src="../includes/js/controller/ItemController.js"></script>
    <script type='text/javascript' src='../includes/js/Jquery/InviteP.js'></script>
</head>

<body>
           <form id='inviteFriend'>
           <input value="{$company['product_id']}" class='mainResultsInfo' name='iProduct'></input>
           <input value='{$userName}' class='mainResultsInfo' name='uProduct'></input>
           <input value='1' class='mainResultsInfo' name='sProduct'></input>
           <input type='image' src='../img/EC%20-%20Add%20plus.png' class='mainResultsConnect' name='sendInivitation'>
           </input>
           </form>
</body>

The intention was to take the form data with this Jquery, already pluged-in in ITEM.PHP

// InviteP.js
$(document).ready(function(){
$('input[name="sendInivitation"]').click(function() {
    var url = "../includes/server/inviteProduct.php";

    $.ajax({
           type: "POST",
           url: url,
           data: $("#inviteFriend").serialize(),
           success: function(data)
           {
               alert("Listo !");
           }
         });

    e.preventDefault();
});
});

Then this one will send information to database through InviteProduct.php

// InviteProduct.php
<?php
    SESSION_START();
    if(isset($_SESSION["user"]))
        {
        $data['session'] = true;
        }

    $data;
    $data['info'] = "";
    $data['session'] = "";

    $tmOp = $_SESSION["user"];

    $dsn = 'mysql:dbname=asd;host=localhost;port=3306';
    $user = 'root';
    $password = '';
    $options = array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
    );

    try {
         $connection = new PDO($dsn, $user, $password, $options);
    } catch (PDOException $e) {
        echo 'Fallo conexión: '. $e->getMessage();
    }


    $iProduct=$_POST['iProduct'];
    $uProduct=$_POST['uProduct'];
    $sProduct=$_POST['sProduct'];

    $sql = "INSERT INTO products_request (product_id, product_requested_id, send_request) VALUES ('$iProduct','$uProduct','$sProduct');
";
    $addCompany = $connection->prepare($sql);
    $addCompany->execute();


?>

Aucun commentaire:

Enregistrer un commentaire