Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como modificar o script PHP/Jquery/Ajax para ter mais de um post de campo de formulário


não sou especialista em php, mas isso deve ajudá-lo:

Primeiro altere a área do formulário na página principal:
<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>

então seu myData fica assim:
var myData = {
    content_txt: $("#contentText").val(),
    balance: $("#balance").val(),
    acctNum: $("#acctNum").val(),
    monthly: $("#monthly").val()
};

e mais tarde na resposta do ajax:
$("#contentText").val(''); //empty text field after successful submission
$("#balance").val('');
$("#acctNum").val('');
$("#monthly").val('');

e finalmente o 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();
}