-1

Friends,

I'm a newbie to PHP.

I've had a problem to deal with that I couldn't understand, so I posted it in this thread.

I've dynamically created 2 textboxes and a button.

  1. Question ID text field

  2. Question text field

  3. Change Button

for the change button I need to write a 'onclick' javascript to pass Question ID

and Question value to a PHP function (set_id) written inside the Same file. In fact that’s why i

Called Form action $_SERVER[“PHP_SELF”].

Here’s my code.

<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);
        } 
    }

?>

Thank You ! Sorry If there was any mistake.

1
  • You can put your PHP code in a separate file, and use AJAX to send data to it and it will return you the values, without your page being refreshed. Commented Mar 18, 2014 at 6:09

1 Answer 1

1

Try this code

<?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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.