【问题标题】:How do i call an ajax file in laravel我如何在 laravel 中调用 ajax 文件
【发布时间】:2018-10-31 00:32:14
【问题描述】:

我有一个保存为 ajax.php 的 ajax 文件,其中包含以下代码:

<?php
// Start the session
session_start();

$con=mysqli_connect("localhost","root","","quiz"); // change here to your data
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Check the number of all questions, if next_id is more than last question, back to first or whatever you want;
$response=mysqli_query($con,"select * from questions");
$number_of_all_questions = mysqli_num_rows($response);

if($_POST['next_id'] == 0){
	// reset to default
	$_SESSION["correct_score"] = 0;
	$_SESSION["not_correct_score"] = 0;
}


if($number_of_all_questions <= $_POST['next_id']){
	// Quiz finished, show results
    echo"<div>
	<h2>Results:</h2>
	<p>Correct answers: {$_SESSION['correct_score']}</p>
	<p>Wrong answers: {$_SESSION['not_correct_score']}</p>

	</div>";



}else{

	// query next question
	$response=mysqli_query($con,"select * from questions WHERE id =(select min(id) from questions where id > {$_POST['next_id']})");
	?>

	<?php while($result=mysqli_fetch_array($response,MYSQLI_ASSOC)){ ?>

		<div id="question_<?= $result['id'] ?>" class='question' data-next-question="<?= $_POST['next_id'] ?>"> <!--check the class for plurals if error occurs-->
			<h2><?= $result['id'].".".$result['question_name'] ?></h2>
			<div class='align'>
				<input type="radio" value="1" id='radio1' name='1'>
				<label id='ans1' for='radio1'><?= $result['answer1'] ?></label>
				<br/>
				<input type="radio" value="2" id='radio2' name='2'>
				<label id='ans2' for='radio2'><?= $result['answer2'] ?></label>
				<br/>
				<input type="radio" value="3" id='radio3' name='3'>
				<label id='ans3' for='radio3'><?= $result['answer3'] ?></label>
				<br/>
				<input type="radio" value="4" id='radio4' name='4'>
				<label id='ans4' for='radio4'><?= $result['answer4'] ?></label>
			</div>
			<br/>
			<?php /*<input type="button" data-next-question="<?= $_POST['next_id'] ?>" id='next' value='Next!' name='question' class='butt'/> */?>
		</div>
	<?php }?>
<?php }?>
<?php mysqli_close($con); ?>

ajax 文件包含从我的数据库“测验”中提取问题的代码。我使用 PHP 和 HTML 做到了这一点,并且效果很好。现在我将其转移到 laravel。问题是,只有普通的 html 显示在视图刀片中。问题未显示在刀片​​中。我怎样才能在我的视图刀片中调用这个 ajax 文件

【问题讨论】:

  • 你应该将你的 ajax.php 代码移动到控制器并通过路由访问它
  • 我是 laravel 的新手,你能解释一下吗?我会用我的 ajax 代码更新它
  • laravel.com/docs/5.6/controllerslaravel.com/docs/5.6/routing 是我能给你的所有帮助,你需要先学习基础知识
  • 这个文件怎么称呼?这是一个 ajax 调用吗?

标签: php html sql ajax laravel


【解决方案1】:

你应该在你的控制器中移动你的 php 代码并通过路由访问它,但是你应该使用 eloquent ORM 从你的数据库中获取数据并且只为你的刀片视图准备变量并显示它们。

here is a tutorial

laravel docs is another choice

【讨论】:

    猜你喜欢
    • 2018-07-19
    • 1970-01-01
    • 2017-10-19
    • 2014-06-15
    • 2019-07-24
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多