dongqiao8502 2014-11-23 10:55
浏览 85

使用jQuery将非表单数据插入数据库

I have a function in JavaScript with the following parameters below:

insertdata(micheal, newyork)

I want to be able to insert thus:

name = micheal
location = newyork 

into my MySQL database using PHP and jQuery.

Both items of data are not coming from a form. I'm only passing them from JavaScript.

  • 写回答

1条回答 默认 最新

  • du6333137 2014-11-23 11:13
    关注

    Since I cannot comment due to reputation, I suggest you a solution that through jQuery you can append this to your current form as hidden fields and these get submitted at the PHP backend and you can save it in your DB.

    Let me know if you need help on how to append it to the form.

    var name = $("<input type='hidden' name='whatever' value='micheal' />");
    $("#myform").append(name);
    

    Also have a look at this

    Add elements using jQuery dynamically

    To process this in PHP see this,

    $variable_name_1 = $_POST['name_of_form_field_1'];
    $variable_name_2 = $_POST['name_of_form_field_2'];
    
    //Read how to sanitize variables
    $query = "INSERT INTO table_name (field1, field2) VALUES ('$variable_name_1', '$variable_name_2')";
    $result = mysqli_query($query);
    if(!$result) {
       echo "Error" . mysqli_error();
    }
    else {
      echo "Done!";
    }
    

    You can have a snapshot of your form submitted by doing this var_dump($_POST)

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效