Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment modifier le script PHP/Jquery/Ajax pour avoir plus d'un champ de formulaire posst

je ne suis pas un expert en php, mais ceci devrait vous aider :

Modifiez d'abord la zone de formulaire sur la page principale :

<div class="form_style">
    <textarea name="content_txt" id="contentText" cols="45" rows="5"></textarea><br/>
    <input type="text" id="balance" /><br/>
    <input type="text" id="acctNum" /><br/>
    <input type="text" id="monthly" /><br/>
    <button id="FormSubmit">Add record</button>
</div>

alors votre myData ressemble à ceci :

var myData = {
    content_txt: $("#contentText").val(),
    balance: $("#balance").val(),
    acctNum: $("#acctNum").val(),
    monthly: $("#monthly").val()
};

et plus tard dans la réponse ajax :

$("#contentText").val(''); //empty text field after successful submission
$("#balance").val('');
$("#acctNum").val('');
$("#monthly").val('');

et enfin le PHP :

//sanitize post value, PHP filter FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH
$content = filter_var($_POST['content_txt'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$balance = filter_var($_POST['balance'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$account = filter_var($_POST['acctNum'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$monthly = filter_var($_POST['monthly'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);


$qry= "INSERT INTO add_delete_record(content,balance,account,monthly) VALUES('".$content."','".$balance."','".$account."','".$monthly."')";


// Insert sanitize string in record
if(mysql_query("INSERT INTO add_delete_record(content,balance,account,monthly) VALUES('".$content."','".$balance."','".$account."','".$monthly."')"))
{
    //Record is successfully inserted, respond to ajax request
    $my_id = mysql_insert_id(); //Get ID of last inserted record from MySQL
    echo '<li id="item_'.$my_id.'">';
    echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$my_id.'">';
    echo '<img src="images/icon_del.gif" border="0" />';
    echo '</a></div>';
    echo $content.'</li>';
    mysql_close($connecDB);

}else{
    //output error

    //header('HTTP/1.1 500 '.mysql_error());
    header('HTTP/1.1 500 Looks like mysql error, could not insert record!');
    exit();
}