【问题标题】:refreshing page after submitting form to display result of the action made提交表单后刷新页面以显示所做操作的结果
【发布时间】:2017-12-08 18:49:30
【问题描述】:

我认为我的代码有问题,我有一个显示可用工作列表的页面,其中有两个按钮一个显示工作资料“个人资料”,第二个用于申请工作“申请”,预期结果当点击申请时,申请的工作将从所有可用工作的列表中消失。该代码在 localhost 上运行良好,但是在将其上传到服务器后,我必须在单击应用后刷新页面才能查看操作的结果。这是AvailableJobs.php的代码:

 <?php $sql1= "SELECT * FROM Job"; // select all the Job
$result1= mysqli_query($conn,$sql1);                 
while($row1=mysqli_fetch_array($result1))
{ $JobName= $row1['JobName'];
    $JID= $row1['JobID']; 
    $OrgID= $row1['OrgID']; // get the Org ID for the available Job 

$sql10=" SELECT * FROM JobStudent WHERE JobID='$JID' AND StudentID='$SID'";
$resultss = mysqli_query($conn,$sql10);
$numResults = mysqli_fetch_array($resultss);
if ($numResults == 0) { 

 $sql2="SELECT * FROM RequestedOrganization WHERE OrgID='$OrgID'"; 
$result2= mysqli_query($conn,$sql2);                     
  while($row2=mysqli_fetch_array($result2))
{ $OrgGPA= $row2['GPA'];
    $OrgTrack= $row2['Track']; 
    $Priority= $row2['priorityGT'];
} 
 ?>  
<li><input disabled style="border:none; margin-bottom: 10px; width:100%;" name="JobName" value="<?php echo $row1['JobName'];?>"> </li> <?php
 echo "<form action='AvailableJobAction_page.php' method='POST'>
<input type='hidden' name='tempId' value='$JID'/>
<input style='border-radius: 12px; margin-bottom: 10px; background-color: #ccc;' type='submit' class='right' name='submit-btn' value='Apply' onclick='return func1()'/>
</form>"; 
echo "<form action='OrgProfileSS.php' method='POST'>
<input type='hidden' name='temppId' value='$JID'/>
<input type='hidden' name='tempppId' value='$OrgID'/>
<input style='border-radius: 12px; background-color: #ccc;' type='submit' name='submit-btnn' class='right' value='Profile'/>
</form><br/>"; ?>

这是执行页面AvailableJobAction_page.php:

$USER=$_SESSION['login_user']; 

if (isset($_POST['submit-btn'])){ 
$Jobid=$_POST['tempId'];  // J ID


$Sql="SELECT * FROM Job WHERE JobID='$Jobid'";
$Result2= mysqli_query($conn,$Sql);
while ( $Row1 =mysqli_fetch_array($Result2)){ 
$JN= $Row1['JobName']; 
$orgid= $Row1['OrgID']; 

$Sql10="SELECT * FROM Employer WHERE EmployerID='$orgid'";
$Result10= mysqli_query($conn,$Sql10);
while ( $Row10 =mysqli_fetch_array($Result10)){ 
$EmpEmail= $Row10['EmailAccount']; 
}  

}     

$sql="SELECT * FROM Student WHERE KsuEmailStuent='$USER'"; // Get S ID 
$result2= mysqli_query($conn,$sql);
while ( $row1 =mysqli_fetch_array($result2)){ 
$StID= $row1['StudentID']; 
}  

$sql4="SELECT * FROM JobStudent WHERE StudentID='$StID' AND JobID='$Jobid'";
$result4= mysqli_query($conn,$sql4);
$rows= mysqli_num_rows($result4);
if ($rows>0){
echo "<script type='text/javascript'>alert('You Already Applied before!')</script>";
echo "<script>window.location.href='AvailableJobs.php';</script>";
}else{ 
$sql4="INSERT INTO JobStudent (JobID,StudentID,Status) VALUES ('$Jobid','$StID','Pending')"; 
$result4= mysqli_query($conn,$sql4);

$sql1="SELECT * FROM Job WHERE JobID='$Jobid'";
$result1= mysqli_query($conn,$sql1);
while ( $row2 =mysqli_fetch_array($result1)){ 
$Nos= $row2['NoOfAppliedstudnets']; 
}   
$Nos++; 
$sql3= "UPDATE Job SET NoOfstudnets='$Nos' WHERE JobID='$Jobid'"; 
$result3= mysqli_query($conn,$sql3);

$to =  $USER; // Send Email with the new Pass 
$subject = "Applied done sucsessfully!";
$message = "Your request have been sent, Please wait for response from employer and note that you can view your request status in your account home";
$message = wordwrap($message,70);
//$headers = "From : Admin@PTN.com";
if(mail($to, $subject, $message)){
echo "<script type='text/javascript'>alert('Applied done sucsessfully!')</script>";
echo "<script>window.location.href='AvailableJobs.php';</script>";
}else{
echo "<script type='text/javascript'>alert('Erorr in send Email ')</script>";
echo "<script>window.location.href='AvailableJobs.php';</script>";
}   

$to =  $EmpEmail; // Send Email with the new Pass 
$subject = "New student have Applied!";
$message = "New student have Applied to this Job ".$JN ;
$message = wordwrap($message,70);
//$headers = "From : Admin@PTN.com";
mail($to, $subject, $message);  
}
}   
else echo "<script type='text/javascript'>alert('ERROR!')</script>";

【问题讨论】:

  • 欢迎来到 Stackoverflow!你的代码一团糟,我不想读。
  • 谢谢,大部分时候我会尽量收拾干净,但不幸的是,这次我没有时间。这取决于你,但我希望其他人可以提供帮助
  • 这不能回答你的问题,因为我没有通读你的代码,但你的代码容易受到 SQL 注入的攻击。使用parameterized queries

标签: javascript php html css mysql


【解决方案1】:

修改部分代码:

$sql10=" SELECT * FROM JobStudent WHERE JobID='$JID' AND StudentID='$SID'";
$resultss = mysqli_query($conn,$sql10);

将检查更改为第一个 mysqli_num_rows 而不是 mysqli_fetch_array

$numResults = mysqli_num_rows($resultss);
if ($numResults == 0) { 

【讨论】:

    猜你喜欢
    • 2020-05-07
    • 2019-03-25
    • 2012-05-25
    • 1970-01-01
    • 2019-05-01
    • 2013-09-21
    相关资源
    最近更新 更多