php online exam javascript questions not incrementing - javascript

actually i am creating a online exam webapplication in php. and i m retrieving data from database .now the prob. is how to loop data by indexing one by one..
so user view one question at a time and after clicking on submit button it display the next question by from array.
here i used array of all the rows and taking question one by one by javascript . but there is some prob. in javascript . i only render 1st question .. increment is not implementing correctly.
<input type="hidden" value="<?php echo $i=0; ?>" id="cc">
<div id="counter">
<p id="idIndex" name="id" value="<?php echo $findData[$i]['Exam']['id']; ?>"><?php echo $findData[$i]['Exam']['id']; ?> </p>
<p ><?php echo $findData[$i]['Exam']['que'];?></p>
<input type="radio" value="<?php echo $findData[$i]['Exam']['op1']; ?>" name="op1" id="jaid" ><?php echo $findData[$i]['Exam']['op1']; ?>
<input type="radio" value="<?php echo $findData[$i]['Exam']['op2']; ?>" name="op1" id="jai" ><?php echo $findData[$i]['Exam']['op2']; ?>
<input type="radio" value="<?php echo $findData[$i]['Exam']['op3']; ?>" name="op1" id="jai"><?php echo $findData[$i]['Exam']['op3']; ?>
<input type="radio" value="<?php echo $findData[$i]['Exam']['op4']; ?>" name="op1" id="jai" ><?php echo $findData[$i]['Exam']['op4']; ?>
</div>
<input type="submit" id="sub" onclick="myexam()" >
<?php echo $this->Html->script('jquery-2.2.3.min'); ?>
<script>
function myexam(value)
{
var v=$("#cc").val();
}
</script>

PHP is server side. Once your HTML is rendered, PHP has nothing more top do with it!
$("#cc").val("<?php echo $i=+1;?>");
The php echo is a string, that wont work.
var v=$("#cc").val();
v = v + 1;
$("#cc").val(v);
That should work. Fix the other line of JS similarly.

Related

OpenCart Set language through URL

When I select my language from the dropdown flag the opencart translates to the new language and I get directed to the index page.
I want to stay on the current page.
I found an article for this issue but I couldn`t make it work.
Link
This is my current code from catalog/controller/module/language.php
<?php if (count($languages) > 1) { ?>
<!-- Language -->
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="language_form">
<div class="dropdown">
<?php foreach ($languages as $language) { ?>
<?php if ($language['code'] == $code) { ?>
<?php echo $language['name']; ?>
<?php } ?>
<?php } ?>
<ul class="dropdown-menu">
<?php foreach ($languages as $language) { ?>
<li><?php echo $language['name']; ?></li>
<?php } ?>
</ul>
</div>
<input type="hidden" name="code" value="" />
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</form>
<?php } ?>

onclick javascript in input tag that will get the id value in the database

I have a code that is already working for a different function, I want to do the same in another way.
Here is the code that I want to somewhat imitate or being a guide:
<td>
<a href="javascript:edit_id('<?php echo $row[0]; ?>')">
<img src="b_edit.png" align="center" />
</a>
</td>
with a function:
function edt_id(id){
if(confirm('Sure to edit ?')){
window.location.href='newread.php?edit_id='+id;
}
}
How can I do it in a input tag using onclick javascript?
Here is the code of the input tag that I want the javascript of getting the ID that is on the database:
<td>
<input placeholder="<?php echo $row[3]; ?>" name="pre" type="number" value="<?php echo $pres; ?>" class="validate" onclick="?????????" min="<?php echo $row[2]; ?>" required>
</td>
I place question marks where I want the javascipt to be placed. Thanks!
use onclick or onblur method to invoke edit_id()function.
Like
<td>
<input placeholder="<?php echo $row[3]; ?>" name="pre" type="number" value="<?php echo $pres; ?>" class="validate" onclick="javascript:edit_id('<?php echo $row[0]; ?>')" onblur="javascript:edit_id('<?php echo $row[0]; ?>')" min="<?php echo $row[2]; ?>" required>
</td>
It seems you are missing i in "edt_id(id)" function
function edit_id(id){
if(confirm('Sure to edit ?')){
window.location.href='newread.php?edit_id='+id;
}
}
and replace "question marks" with edit_id('')

Adding labels for radio-boxes in Magento

Trying to use label for each radio-box(to customise them with css3), but there is javascript preventing the labels to work as they should. When I click on a label, the radio-box gets focused but not checked.
If you click straight on the radio-box, it validates and adds a class .validation-passed to the input. Maybe I need to make a validation for the label aswell?
Anyone faced this before and could help out to make it work?
<table class="data-table review-summary-table" id="product-review-table">
<tbody>
<?php foreach ($this->getRatings() as $_rating): ?>
<tr>
<td class="label">
<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>
</td>
<?php foreach ($_rating->getOptions() as $_option): ?>
<td class="value">
<label for="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>">
<?php echo $this->escapeHtml($_rating->getRatingCode()) ?> <?php echo $_option->getValue() ?>
</label>
<input type="radio" name="ratings[<?php echo $_rating->getId() ?>]" id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>" value="<?php echo $_option->getId() ?>" class="radio" />
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="hidden" name="validate_rating" class="validate-rating" value="" />
<script type="text/javascript">decorateTable('product-review-table')</script>
I see that all of radio boxes have different names.
If you want collate it to 1 group you should use same names for all radio buttons.
name="ratings"

"if" condition variable in PHP

I am building a quiz app, working fine now decided to add "boosters" (help tools for the player), I've already posted a question here about adding extra time to the timer, I am still digging to find a solution, but no luck til now.. Anyway, this is not my question now.
I started to work on the second booster: Successfully skip a question (without losing points)
So the original code (before adding the boost part) was like this:
<div id='question<?php echo $i;?>' class='cont'>
<p class='questions' id="qname<?php echo $i;?>">
<?php echo $i?>.<?php echo $result['question_name'];?></p>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer1'];?><br/>
<input type="radio" value="2" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer2'];?><br/>
<input type="radio" value="3" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer3'];?><br/>
<input type="radio" value="4" id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/>
<?php echo $result['answer4'];?><br/>
<input type="radio" checked='checked' style='display:none' value="5"
id='radio1_<?php echo $result['id'];?>'
name='<?php echo $result['id'];?>'/><br/>
<button id='<?php echo $i;?>' class='next btn btn-success' type='button' >Next</button>
And this is how the points are count:
if(isset($_POST[1])){
$keys=array_keys($_POST);
$order=join(",",$keys);
//$query="select * from questions id IN($order) ORDER BY FIELD(id,$order)";
// echo $query;exit;
$response=mysql_query("select id,answer from questions where id IN($order) ORDER BY FIELD(id,$order)") or die(mysql_error());
$right_answer=0;
$wrong_answer=0;
$unanswered=0;
while($result=mysql_fetch_array($response)){
if($result['answer']==$_POST[$result['id']]){
$right_answer++;
}else if($_POST[$result['id']]==5){
$unanswered++;
}
else{
$wrong_answer++;
}
}
So my idea was about adding a button called "Success skipe"
<button id='<?php echo $success;?>' class='skip btn btn-success' type='button'>Success Skip</button>
And while counting the right answers I tried to make a condition that once this button is clicked the answer is considered correct so I added this:
while($result=mysql_fetch_array($response)){
if($success){ //Just added
$right_answer++; //this "if" condition
}
But it didn't work, what's wrong?

javascript data to form hidden file in php

In my mvc application i used raty for rating my image. Here is my code
<div class="container">
<form method="post" class='form' role='form' action="?section=photo&view=addVote">
<input type="hidden" name="photoId" value="<?php echo $photoinfo['id']; ?>"></input>
<input type="hidden" name="score" value="<?php ?>"></input>
<div class="row">
<div>
<img src="<?php echo base_url(); ?>images/photo_booth/<?php echo $photoinfo['image_name'];?>" class="img-rounded img-define" width="580" height="350"></img>
</div>
<div>
<h6><?php echo $photoinfo['title'] ?></h6>
</div>
<div>
<p class="paragraph"><?php echo $photoinfo['description'] ?></p>
</div>
<div>
<div id="raty"></div>
<button type="submit" class="btn btn-primary button_new submit-btn">Add Vote</button>
</div>
</div>
</form>
<script type="text/javascript">
$('#raty').raty({
score: <?php echo $photoinfo['vote']; ?>,
number: 10,
starOn: "<?php echo base_url(); ?>icons/star-on.png",
starOff: "<?php echo base_url(); ?>icons/star-off.png",
readOnly: false
});
$("#raty > img").click(function(){
var score=$(this).attr("alt");
});
</script>
Here i can save my score=$(this).attr("alt"); to database using javascript. But i need it to be passed as a hidden field data something like this <input type="hidden" name="score" value="<?php the score of raty! ?>"></input>
. Here addVote is a method where i have to do the operation.
can i do that? if another way exist so how?Thanks.
Write php inside javascript as string can solve your problem:-
score: "<?php echo $photoinfo['vote']; ?>"
you forgot a " " in your php
score: <?php echo $photoinfo['vote']; ?>,
you can use score: "<?php echo $photoinfo['vote']; ?>",
instend of score: <?php echo $photoinfo['vote']; ?>,

Categories