【问题标题】:Values to PHP function parameters from Button click Javascript functionButton click Javascript 函数中 PHP 函数参数的值
【发布时间】:2014-03-18 14:59:04
【问题描述】:

朋友们,

我是 PHP 新手。

我有一个我无法理解的问题要处理,所以我将它发布在这个帖子中。

我已经动态创建了 2 个文本框和一个按钮。

  1. 问题 ID 文本字段

  2. 问题文本字段

  3. 更改按钮

对于更改按钮,我需要编写一个“onclick”javascript 来传递问题 ID

和问题值写入同一文件中的 PHP 函数 (set_id)。其实这就是为什么我

调用表单操作 $_SERVER[“PHP_SELF”]。

这是我的代码。

<html>
<head>
<script>


function getvalue(value)
{
    var qid_value = 'qid_'+value.substring(4);

    alert('QID = '+ document.getElementById(qid_value).value + ' QUESTION = ' + document.getElementById(value.substring(4)).value);

/*
I created this javascript alert to test the id s of textboxes and their values
*/

}
</script>
</head>

<body>

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">

<!-- These fields are dynamically created -->

<input type="text" id="'.$var_id.'" name="'.$var_id.'" value="'.$row['qid'].'" readonly size="2 px"/>

<input type="text" id="'.$var_question.'" name="'.$var_question.'" value="'.$row['question'].'" style="size:auto"/>

<input type="button" id="'.$var_question.'" name="'.$var_question.'" value="Change" onclick="getvalue(this.name)"/> 

<!-- These fields are dynamically created -->

</form>

</body>
</html>

<?php

    $msg= "";

    function display($qid,$question)
    {
           require('uni_db_conn.php'); // this is my db connection          
           $qid = $_POST[$qid];       
           $question= $_POST[$question];
           $query = "UPDATE question SET question='.$question.' WHERE qid='.$qid.'";
           $result = mysql_query($query);

           if(!$result)
           {
            $msg= 'Cant Insert Values to the Table !'.mysql_error();
           }
           else
           {
            $msg = 'Successfully Added to the Table !';
           }

           echo '<label>'.$msg.'</label>';
    }


    function set_id($qid,$question)
    {
        if(isset($_POST[$question]))
        {
           display($qid,$question);
        } 
    }

?>

谢谢!如有错误请见谅。

【问题讨论】:

  • 您可以将您的 PHP 代码放在一个单独的文件中,并使用AJAX 向其发送数据,它会返回您的值,而无需刷新您的页面。

标签: javascript php html mysql web


【解决方案1】:

试试这个代码

<?php
if(isset($_POST['submit'])){
$QID = $_POST["qid"];
$QUE = $_POST["question"];
echo $QID;
echo $QUE;
}
?>

<html>
<head>
<script language="javascript">


function getvalue()
{
var valid= true;
var id = document.getElementById("ID").value;
var ques = document.getElementById("ques").value;
return valid;
}
</script>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onSubmit=" return getvalue()" >

<input type="text" id="ID" name="qid"/>
<input type="text" id="ques" name="question"/>
<input type="submit" name="submit" value="Change"/> 

</form>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2011-09-01
    • 2014-05-10
    相关资源
    最近更新 更多