简体   繁体   English

Javascript / PHP交互仅在第二次单击时出现

[英]Javascript/PHP interaction only appears on second click

Have searched for the answer but no joy, so here goes... 寻找答案,但没有喜悦,所以这里...

I'm working on a mobile hybrid app. 我正在开发移动混合应用程序。 I want the user to fill in their id number, which is then submitted to a javascript function for basic validation, then kicks in a jQuery.getJSON request to my serverside PHP which returns the data and then my jQuery will repopulate the form div with the data. 我希望用户填写其ID号,然后将其提交给javascript函数以进行基本验证,然后向服务器端PHP发起一个jQuery.getJSON请求,该请求将返回数据,然后我的jQuery将用数据。

Currently it doesn't really work at all in Firefox, and only works correctly in Safari after I press the submit button for a second time. 目前,它实际上根本无法在Firefox中使用,只有在我再次按下提交按钮后才能在Safari中正常使用。 It returns the correct data, so the link is ok. 它返回正确的数据,因此链接正常。

My problem is: Why does the div not get repopulated after the first click? 我的问题是:为什么第一次单击后div不会重新填充?

HTML: HTML:

<div id="login540div">
<form id="login540form" onSubmit="login540()">
Enter Student ID number<input type="text" name="login540id" id="login540id"/>
<input type="submit" value="Submit" />
</form>
</div>

Javascript: Javascript:

function login540(){
// validates the data entered is an integer. 
var loginNo = document.getElementById("login540id").value;
//if(!isNaN(loginNo))
 if((parseFloat(loginNo) == parseInt(loginNo)) && !isNaN(loginNo))
{

 //jSonCaller(loginNo); 
 $.getJSON('http://localhost:8888/c05673160/login540.php?q='+loginNo, function(data){

//alert(data);
$('#login540div').html("<p>First Name ="+
data.firstName+
"</p> Last Name ="+data.lastName+" </p>Module No.1 ="+
data.moduleNo1+"</p> Module No.2 ="+
data.moduleNo2+"<p> Course ID="+
data.courseID+"</p>");
    })

}
  else 
    {
    // alert(loginNo);  CHECKED
    alert("Please make sure to insert only a whole number");
        } 

Then the PHP goes like this... 然后PHP像这样...

    <?php
include ("config.php");
/*
require_once('FirePHPCore/FirePHP.class.php');
ob_start();
$firephp = FirePHP::getInstance(true);

$var = array('i'=>10, 'j'=>20);

$firephp->log($var, 'Iterators');
*/

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'collegeData';

$q=$_GET["q"];

$table = "studentTable";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$conn)     
    die('Could not connect: ' . mysql_error());
if (!mysql_select_db($dbname))    
    die("Can't select database");   

$result = mysql_query("SELECT * FROM {$table} WHERE studentID = '".$q."'");
if (!$result)  
    die("Query to show fields from table failed!" . mysql_error());

$json = array();
while($row = mysql_fetch_array ($result))     
{
    $json = array(
        'firstName' => $row['firstName'],
        'lastName' => $row['lastName'],
        'moduleNo1' => $row['moduleNo1'],
        'moduleNo2' => $row['moduleNo2'],
        'courseID' => $row['courseID']
    );
}

$jsonstring = json_encode($json);
echo $jsonstring;

mysql_close($conn);
?>

I can't figure out what's wrong, and I've been messing around with various things for the last few days trying to fix it, but no joy, so I'd really appreciate any help you can give. 我无法弄清楚出了什么问题,最近几天我一直在忙于解决各种问题,但没有任何乐趣,因此,我非常感谢您能提供的任何帮助。

The bottom of Function login540() is missing, but this needs to kill the default "submit" action: this can be done with "return false;" 缺少函数login540()的底部,但这需要取消默认的“提交”操作:可以通过“ return false;”来完成。 at the end. 在最后。 As I can't see the end, not sure if this happens or not, but the form probably "submits" while the AJAX is running. 我看不到结尾,不确定是否会发生这种情况,但是在AJAX运行时表单可能会“提交”。

This is compounded as the form doesn't have an action, which probably explains the different browser behaviour. 由于表单没有动作,这很复杂,这可能解释了不同的浏览器行为。 I suggest setting action to "#" and then, if you see "#" added in the URL then the form has submitted and not been stopped by your function. 我建议将操作设置为“#”,然后,如果您看到URL中添加了“#”,则表示表单已提交并且未被您的函数阻止。

@Robbie had a good point that you don't appear to be stopping the default behavior of the form submission. @Robbie指出,您似乎并没有停止表单提交的默认行为。 To do this you need to change a couple things: 为此,您需要更改以下几项:

  1. onSubmit="login540()" needs to change to onSubmit="return login540()" otherwise whatever you return from the login540() function will be ignored. onSubmit="login540()"需要更改为onSubmit="return login540()"否则您从login540()函数返回的任何内容都将被忽略。
  2. At the end of the login540() function you need to return false; login540()函数的最后,您需要return false; to stop the form from submitting normally. 阻止表单正常提交。 You can also pass in the event object as the first argument and use event.preventDefault() instead: function login540(event){event.preventDefault();...} . 您还可以将event对象作为第一个参数传递,并使用event.preventDefault()代替: function login540(event){event.preventDefault();...}

To do yourself a favor however, you can use jQuery to bind the submit event handler to the form rather than using inline JS (tisk, tisk, :) ) 为了帮自己一个忙,您可以使用jQuery将submit事件处理程序绑定到表单,而不是使用内联JS(tisk,tisk,:))

$('#login540form').on('submit', login540);

This way you can keep all of your JS in one place rather than spread-out all over your HTML. 这样,您可以将所有JS放在一个位置,而不是扩展到整个HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM