Mysql
 sql >> Database >  >> RDS >> Mysql

Come modificare lo script PHP/Jquery/Ajax per avere più di un campo modulo post

non sono un esperto di php, ma questo dovrebbe farti passare:

Per prima cosa cambia l'area del modulo nella pagina 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>

quindi i tuoi dati personali avranno questo aspetto:

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

e più avanti nella risposta ajax:

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

e infine il 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();
}