gpt4 book ai didi

PHP/JS : Posting input value with js function and echoing through php

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:24 24 4
gpt4 key购买 nike

我的目标是让输入字段能够获取输入内容的值(value)并通过回显。 js函数会在用户停止输入两秒后抓取输入框的值并发布。问题似乎是 php 没有回应输入框的值。当我取出 js 函数并使用强制刷新的按钮时它工作正常。为什么 php 没有获取 js 函数发布的值?

Example SITE

JS

            <script type="text/javascript">
$(document).ready(function() {
var timer;
$('#video-input1').on('keyup', function() {
var value = this.value;

clearTimeout(timer);

timer = setTimeout(function() {

//do your submit here
$("#ytVideo").submit()

//alert('submitted:' + value);
}, 2000);
});


//then include your submit definition. What you want to do once submit is executed
$('#ytVideo').submit(function(e){
e.preventDefault(); //prevent page refresh
var form = $('#ytVideo').serialize();
//submit.php is the page where you submit your form
$.post('index.php', form, function(data){

});
return false;

});


});
</script>

PHP

<?php

if($_POST)
{
$url = $_POST['yurl'];


function getYoutubeVideoID($url) {
$formatted_url = preg_replace('~https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/| youtube\.com\S*[^\w\-\s])([\w\-]{11})
(?=[^\w\-]|$)(?![?=&+%\w]*(?:[\'"][^<>]*>| </a>))[?=&+%\w-]*~ix','http://www.youtube.com/watch?v=$1',$url);
return $formatted_url;
}
$formatted_url = getYoutubeVideoID($url);
$parsed_url = parse_url($formatted_url);




parse_str($parsed_url['query'], $parsed_query_string);
$v = $parsed_query_string['v'];

$hth = 300; //$_POST['yheight'];
$wdth = 500; //$_POST['ywidth'];
$is_auto = 0;


//Iframe code with optional autoplay

echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$v.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');

}
?>

表单

<html>
<form method="post" id="ytVideo" action="">
Youtube URL:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="video-input1" type="text" value="<?php $url ?>" name="yurl">
<input type="submit" value="Generate Embed Code" name="ysubmit">
</form>
</html>

最佳答案

这是因为您没有在任何地方返回您的 php 结果。它只是丢失了...

$.post('index.php', form, function(data){ 
var x = $(data);
$("body").html(x);
});

关于PHP/JS : Posting input value with js function and echoing through php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11465335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com