由于此语句“ $ request-> get('pos')”,我得以解决索引问题“ pos” 我能够在ajax查询中恢复正确的光标位置,但在控制器中始终将其重置为0。我不知道此问题的出处以及如何解决它,因为这是我使用ajax的第一个。 我真的不明白它是如何工作的 谢谢你的理解! 这是我的新脚本: 这是我的模板:new.html.twig
{% extends '::layoutv.html.twig' %}
{%块样式表%} {%endblock%}
{%滴定度%} {%endblock%}
{%block body%}
<div>
<h1>
Insertion des projets
</h1>
</div>
<!-- BEGIN DISPLAY THE FLASH MESSAGES -->
{% for flashMessage in app.session.flashbag.get('error') %}
<div class="alert alert-error">
<button class="close" data-dismiss="alert"></button>
<strong><i class='icon-remove'></i> </strong> {{ flashMessage }}
</div>
{% endfor %}
<!-- END DISPLAY THE FLASH MESSAGES -->
<div class="row-fluid">
<div class="span6">
<div class="portlet-body form">
<!-- BEGIN FORM-->
<form id="formProject" class="form-horizontal"
action="{{ path('new_project') }}" enctype="multipart/form-data" method="post" }>
<div class="control-group">
<label class="control-label">Télécharger une image</label>
<div class="controls">
<input type="file" name="image">
</br>
<div class="row">
<div class="col-sm-8" onClick="showWord()">
{{ form_row(formProject.texte|raw) }}
</div>
</div>
</div>
</br>
</div>
<div class="controls margin-bottom-30">
<button type="submit" name="submit" class="btn green">Valider</button>
<button type="reset" class="btn yellow">Annuler</button>
</div>
{{ form_rest(formProject) }}
</form>
<!-- END FORM-->
</div>
</div>
</div>
{%endblock%}
{%block javascripts%}
<script type="text/javascript">
function showWord() {
cursorPos = document.getElementById("formation_minipbundle_project_texte");
start = cursorPos.selectionStart;
$.ajax({
type: "POST",
async: false,
data:{'pos' : start},
url: "{{ path('new_project') }}",
success: function() {
console.log(start);
}
});
}
</script>
{%endblock%}
这是我在控制器中的动作:
/**
* @Route("/create", name="new_project")
*/
public function createAction(Request $request) {
$project = new Project();
$em = $this->getDoctrine()->getEntityManager();
$formProject = $this->createForm('Formation\MiniPBundle\Form\ProjectType', $project);
$formProject->handleRequest($request);
if ($formProject->isValid()) {
if(isset($_POST['submit'])){
$file_name = $_FILES['image']['name'];
// $file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
// $file_type = $_FILES['image']['type'];
move_uploaded_file($file_tmp,$file_name);
header ("Content-type: image/jpeg");
$string=$formProject->get('texte')->getData();
$text= filter_var($string, FILTER_SANITIZE_STRING); //Supprimer les balises.
$font = 15;
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font) ;
switch (strtolower(pathinfo($file_name, PATHINFO_EXTENSION))){
case "jpg" :
$im = imagecreatefromjpeg($file_name);
break;
case "gif" :
$im = imagecreatefromgif($file_name);
break;
case "png" :
$im = imagecreatefrompng($file_name);
break;
case "bmp":
$im = imagecreatefrombmp($file_name);
break;
default :
trigger_error("Error Bad Extention");
exit();
break;
}
$position=intval($request->get('pos'));
$y = imagesy($im) - $height;
$textColor = imagecolorallocate ($im, 255, 255,255);
/*----------------------------------------*/
imagestring($im, $font, $position, $y, $text, $textColor);
$project->setLogo($file_name);
$em->persist($project);
$em->flush();
imagejpeg($im);
}
}
return $this->render('FormationMiniPBundle:Project:new.html.twig', array(
'project' => $project,
'request'=> $request->request->get('pos'),
'formProject' => $formProject->createView(),
));
}
答案 0 :(得分:0)
由于您尚未告诉我们您使用的是什么PHP框架,因此在纯PHP中,应该在变量上包含POST数据
$ _ POST ['pos'] 要么 $ _REQUEST ['pos']
但是在您的Javascript代码上,您正在成功打印ajax的开始,即在将值发送到服务器并返回之后...。因此,由于存在明显的延迟,这意味着开始变量ajax之前可以为null。
尝试在此行之后立即使用console.log(开始):
start = cursorPos.selectionStart; console.log(开始);
在制作Ajax之前,因为如果那一点为null,则在服务器上将为null。