Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  I Can Has Kittenz Mar 18 at 6:09
add comment

1 Answer

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>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.