dongle7553 2014-10-27 19:06
浏览 38
已采纳

使用php和ajax驱动的表单更改文本文件

i have created a simple html form with one field and it post to the server side php and the value of the field is saved to a text file.

This is the parts of the code:

Html:

<form action="videorefresh.php" method="POST">
    <input name="videolink" type="text" size="70" />
    <input type="submit" name="submit" value="Save Data">
</form>

php:

<?php

$open = fopen("video.txt","w+");
$txt = "video.txt"; 
if (isset($_POST['videolink'])) { // check if both fields are set
    $fh = fopen($txt, 'a'); 
    $txt=$_POST['videolink']; 
    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file
}

?>

here everythink works fine!

I want to drive all this through Ajax so the main html form wont refresh.

so here is the html:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="./JS/videolink.js"></script>
</head>
<body>
<div id="mainform">
  <div id="form">
    <div>
      <input name="videolink" type="text" id="videolink" size="70">
      <input id="submit" type="button" value="Submit">
</div>
</div>
</div>
</body>
</html>

And here is the js:

$(document).ready(function(){
$("#submit").click(function(){
var name = $("#videolink").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'videolink1='+ videolink ;
if(videolink=='')
{
alert("Please Fill All Fields");
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "./videorefresh.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
}
return false;
});
});

What i do wrong here and it doesnt work?

Please help

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-10-27 19:11
    关注

    I think you want to do is:

    var dataString = '?videolink='+ name;//typo videolink
    // or better put an id on the form and use serialize()
    // var dataString = $('#myform).serialize();
    

    NOT

    var dataString = 'videolink1='+ videolink ;
    

    You have the value of the input in name, videolink is undefined

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献