www.webdeveloper.com
Results 1 to 2 of 2

Thread: Sending input text to mysql

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    2

    Sending input text to mysql

    I have no idea why this won't work. It is wrapped up in a wordpress plugin that I am working on. All of the other stuff works (text area for the question, etc) but the answer value and answer content won't go into the database. In fact, it deletes the answer completely out of the database. If I prefill the data into the database, it displays it into the fields correctly. If I click save it reloads the page with no answers. I've been at this for a few hours now and I am goin crazy. Anyone got an idea of what I am doing wrong here?

    Code:
    <?php
    
    $action = 'new';
    if($_REQUEST['action'] == 'edit') $action = 'edit';
    
    $question= $wpdb->get_row($wpdb->prepare("SELECT question, explanation FROM {$wpdb->prefix}quiz_question WHERE ID=%d", $_REQUEST['question']));
    $all_answers = $wpdb->get_results($wpdb->prepare("SELECT answer,answer_value FROM {$wpdb->prefix}quiz_answer WHERE question_id=%d ORDER BY sort_order", $_REQUEST['question']));
    
    $answer_count = 2;
    if($action == 'edit' and $answer_count < count($all_answers)) $answer_count = count($all_answers) ;
    ?>
    
    <div class="wrap">
    	<script type="text/javascript">
    		var answer_count = <?php echo $answer_count?>;
    
    		function newAnswer() {
    			answer_count++;
    			var para = document.createElement("p");
    			var textarea = document.createElement("textarea");
    			textarea.setAttribute("name", "answer[]");
    			textarea.setAttribute("rows", "3");
    			textarea.setAttribute("cols", "50");
    			para.appendChild(textarea);
    			var input = document.createElement("input");
    			input.setAttribute("type", "text");
    			input.setAttribute("name", "answer_value[]");
    			input.className = "answer_value";
    			input.setAttribute("value", answer_count);
    			input.setAttribute("id", "answer_value_" + answer_count);
    			para.appendChild(input);
    			
    			//$("extra-answers").innerHTML += code.replace(/%%NUMBER%%/g, answer_count);
    			document.getElementById("extra-answers").appendChild(para);
    		}
    		function init() {
    			jQuery("#post").submit(function(e) {
    				
    				// We must have at least 2 answers.
    				var answer_count = 0
    				jQuery(".answer").each(function() {
    					if(this.value) answer_count++;
    				});
    				if(answer_count < 2) {
    					alert("<?php e("Please enter at least two answers"); ?>");
    					e.preventDefault();
    					e.stopPropagation();
    					return true;
    				}
    
    				var valid_answer = false;
    				// Make sure at least one answer has a value more than 0.
    				for (i=0;i<answer_count;i++){
    					if (answer_value > 0){
    						valid_answer = true;
    					}
    				}
    				if(!valid_answer) {
    					alert("<?php e("Please enter more than 0 points to one answer."); ?>");
    					e.preventDefault();
    					e.stopPropagation();
    				}*/
    			});
    		}
    		jQuery(document).ready(init);
    	</script>
    	<form name="post" action="edit.php?page=quizzin/question.php&amp;quiz=<?php echo $_REQUEST['quiz']; ?>" method="post" id="post">
    	<div id="poststuff">
    		<div class="postbox">
    			<h3 class="hndle"><span><?php e('Answers') ?></span></h3>
    			<div class="inside">			
    				<?php for($i=1; $i<=$answer_count; $i++) { ?>
    					<p><textarea name="answer[]" class="answer" rows="3" cols="50"><?php if($action == 'edit') echo stripslashes($all_answers[$i-1]->answer); ?></textarea>
    					<input name="answer_value[]" class="answer_value" type="text" size="2" value="<?php echo stripslashes($all_answers[$i-1]->answer_value); ?>" /></p>
    				<?php } ?>
    				<div id="extra-answers"></div>
    					<a href="javascript:newAnswer();"><?php e("Add New Answer"); ?></a>
    				</div>
    			</div>
    		</div>
    		<p class="submit">
    			<input type="submit" name="submit" value="<?php e('Save') ?>" style="font-weight: bold;" />
    		</p>
    	</div>
    	</form>
    </div>

  2. #2
    Join Date
    May 2012
    Posts
    2
    Because I was probably scaring everyone away with a wall of code I am going to double post!

    Code:
    foreach ($_REQUEST['ID'] as $answer_id) {
    		$answer_text[$counter_answer]=$_REQUEST['answer_text'];		
    			$wpdb->get_results($wpdb->prepare("UPDATE {$wpdb->prefix}quiz_answer SET answer=%s, sort_order=%d WHERE ID=%d",$answer_text[$counter_answer], $counter_answer, $answer_id));
    			wpframe_message($answer_id);
    			wpframe_message($answer_text[$counter_answer]);
    			wpframe_message($counter_answer);
    			//$sort_order_counter++;
    		$counter_answer++;
    	}
    The ID is passed from the form just fine. The output, however, is "answer_id, Array, counter_answer. How can I draw each element from answer_text and display it correctly? wpframe_message should be displaying each answer_text, not all of them at once.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles