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

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改