简体   繁体   中英

PHP jquery ajax onclick?

I'm testing some jquery/ajax thing and I ran into some problems?

Any ideas what I'm doing wrong?

 $output.= '    
                       <div class="save_result">
                        <form id="result_save">
                        <input type="hidden" name="estate_id" value="'.$result_id.'">
                        <input type="hidden" name="user_id" value="'.$useric['id'].'">
                        </form>

            <a href="#" onClick="$.post(\''.$_SERVER['HTTP_HOST'].'/save.php\', $(\'form#result_save\').serialize())" ><img src="images/results/result_save.png" alt="save picture" /> Merken</a>
            </div>'; echo $output;

and in save.php I have

<?php
require_once 'bone/site.php';
$db=new MySQLDatabase();
$site= new Site();


if ((isset($_POST['estate_id']))&&(isset($_POST['user_id']))){
    $id=$db->escape_value($_POST['estate_id']);
    $user_id=$db->escape_value($_POST['estate_id']);

    $sql="INSERT INTO estate_save(`estate`,`user`) VALUES ({$id}, {$user_id});";
    $query=$db->query($sql);


}


?>

edit: I don't get any actual error, but I click on the link and nothing gets written into the database. I tried testing save.php seperatly and it works so something must be wrong with my first file containing jquery.

why you use ajax post, change it to a action attribute and call .submit() on a click:

$output.= '    
    <div class="save_result">
        <form id="result_save" method="POST" action="'.$_SERVER['HTTP_HOST'].'/save.php">
            <input type="hidden" name="estate_id" value="'.$result_id.'" />
            <input type="hidden" name="user_id" value="'.$useric['id'].'" />
            <a href="#" onClick="jQuery(this).parents('form:first').submit(); return false;">
                <img src="images/results/result_save.png" alt="save picture" /> Merken
            </a>
        </form>
    </div>';

echo $output;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM