php - 使用没有post get方法的ajax获取变量值
问题描述
在这里,我有一个包含输入文件类型的简码,我想在 .blur 函数上加载一个 php 文件。
// 输入文件的嵌入位置
<input class="vh-radio-toggle" id="toggle8" type="radio" name="toggle" />
<label class="accordion-label" for="toggle8">I have an inspiration neon sign.</label>
<section id="content8">
<label for="avatar">Send your inspirational custom neon logo image here:</label>
<?php echo do_shortcode('[neon-inspire]');?>
</section>
这是输入文件 html. 我只是用简码制作的。
function misha_uploader_callback(){
return '<input type="file" accept="image/png, image/jpeg" name="profilepicture" size="25" />
<input type="text" name="neon_inspi" value="' . echo $neonInspi; .'">';
}
这是我的 jquery ajax 函数。
jQuery("#neon-inspi").blur(function() {
var url = "http://localhost/neonew/wp-content/themes/twentytwenty/process_upload.php";
jQuery.post(url, {} , function(data) {
// The data here represents the answer from the server
console.log("Data Loaded: " + data);
});
});
这是我的 process_upload.php。我想通过 .blur 函数加载这个 php 文件。我想让 $neonInspi 值返回到输入文件 html 下面的输入字段
<?php
// WordPress environment
require( dirname(__FILE__) . '/../../../wp-load.php' );
$wordpress_upload_dir = wp_upload_dir();
// $wordpress_upload_dir['path'] is the full server path to wp-content/uploads/2017/05, for multisite works good as well
// $wordpress_upload_dir['url'] the absolute URL to the same folder, actually we do not need it, just to show the link to file
$i = 1; // number of tries when the file with the same name is already exists
$profilepicture = $_FILES['profilepicture'];
$new_file_path = $wordpress_upload_dir['path'] . '/' . $profilepicture['name'];
$new_file_mime = mime_content_type( $profilepicture['tmp_name'] );
if( empty( $profilepicture ) )
die( 'File is not selected.' );
if( $profilepicture['error'] )
die( $profilepicture['error'] );
if( $profilepicture['size'] > wp_max_upload_size() )
die( 'It is too large than expected.' );
if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
die( 'WordPress doesn\'t allow this type of uploads.' );
while( file_exists( $new_file_path ) ) {
$i++;
$new_file_path = $wordpress_upload_dir['path'] . '/' . $i . '_' . $profilepicture['name'];
}
// looks like everything is OK
if( move_uploaded_file( $profilepicture['tmp_name'], $new_file_path ) ) {
$upload_id = wp_insert_attachment( array(
'guid' => $new_file_path,
'post_mime_type' => $new_file_mime,
'post_title' => preg_replace( '/\.[^.]+$/', '', $profilepicture['name'] ),
'post_content' => '',
'post_status' => 'inherit'
), $new_file_path );
// wp_generate_attachment_metadata() won't work if you do not include this file
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate and save the attachment metas into the database
wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
// Show the uploaded file in browser
// wp_redirect( $wordpress_upload_dir['url'] . '/' . basename( $new_file_path ) );
$neonInspi = $wordpress_upload_dir['url'] . '/' . basename( $new_file_path ) ;
}
解决方案
推荐阅读
- ios - 我如何将 json 提取到另一个视图控制器
- laravel - Laravel:@section 中的 br
- browser - 如何让我的浏览器连续播放目录中的所有歌曲
- ios - 类型 'Provider' 不符合协议 'IntentTimelineProvider' 错误发生。我添加了小部件
- php - 如何获取主题上的 WordPress 插件版本号?
- python - psycopg2 sql.SQL:组合元素必须是可组合的
- html - CSS:将项目对齐到中心,但忽略换行
- javascript - AntDesign 和 React 问题
- joomla - Joomla 内置注册表单:提交表单后遇到错误
- python - Keras:训练模型时出现“损失:nan - 准确率:0.0000e+00”