I'm a complete beginner with web development and I'm facing a problem. I have a navigation bar, with a search form that 'gets' to search.php
Sample URL : http:/....search.php?s=asdas
I would want to get the value of $_GET['s'] and place it in a textbox with the id = 'filter-searchbox'
My code so far goes like this :
<?php
if(isset($_GET['s'])){
$searchWord = $_GET['s'];
?>
<script>
document.getElementById('filter-searchbar').value =<?php $searchWord ?> ;
</script>
<?php }
?>
<h3> <div class="label label-default"> Search for Events </div></h3>
<BR>
<div id="filter" class="col-md-3">
<input id='filter-searchbar' name='searchWord' type="text" class="form-control" placeholder="Search Events..." value= "" ><br>
Thank you very much!
Rewritten the code, Try this,
<?php
$searchWord="";
if(isset($_GET['s'])){
$searchWord = $_GET['s'];
}
?>
<input id='filter-searchbar' name='searchWord' type="text" class="form-control" placeholder="Search Events..."
value= "<?php echo $searchWord;?>" ><br>
You missed echo and should write this echo "'".$searchWord ."'"; below the input tag.
Because the document.getElementById('filter-searchbar')... statement would executed successfully after only the dom is ready.
<h3> <div class="label label-default"> Search for Events </div></h3>
<BR>
<div id="filter" class="col-md-3">
<input id='filter-searchbar' name='searchWord' type="text" class="form-control" />
<?php
if(isset($_GET['s'])){
$searchWord = $_GET['s'];
?>
<script>
document.getElementById('filter-searchbar').value = <?php echo "'".$searchWord ."'"; ?> ;
</script>
<?php }
?>
document.getElementById('filter-searchbar').value = is missing an echo statement.
Related
I'm new to php and mysql, I'm using a script like CMS, my problem that I can write new article, but when editing it on chrome or opera all of article content is not shown for editing and deleted and it's not the case with firefox, here below the edit php script,
<?php echo link_tag('assets/themes/' . $this->selected_theme . '/'.UIKIT_VERSION.'/css/' . $this->session->userdata('selected-theme')); ?>
<script src="<?php echo base_url() ?>assets/themes/<?php echo $this->selected_theme . '/ckeditor/ckeditor.js'; ?>"></script>
<h4><i class="uk-icon-edit"></i> edit step </h4>
<hr class="uk-article-divider">
<?php $this->load->view('user/template/default/flash_data') ?>
<?php
$attributes = array(
'method' => 'post',
'class' => 'uk-form'
);
echo form_open_multipart('articles/edit/' . $step->step_id, $attributes) ?>
<div class="uk-form-controls">
<label class="uk-form-label">
<?php if ($step->step_photo_url) { ?>
<img src="<?php echo $step->step_photo_url ?>" class="uk-thumbnail-small"/><br/>
<?php } else { ?>
<i class="uk-icon-image uk-icon-large"></i>
<?php } ?>
</label><br/>
<input type="file" name="step_photo_url"/>
<br/>
<label class="uk-form-label uk-text-muted uk-text-bold">Or Enter a URL</label>
<input class="uk-width-90 uk-form-large" type="text" placeholder="Image Url" name="step_photo_url"
value="<?php echo set_value('step_photo_url', $step->step_photo_url, true) ?>"/>
<?php echo form_error('step_photo_url') ?>
</div>
<div class="uk-form-controls uk-margin-top">
<div class="uk-form-controls uk-margin-bottom">
<input value="<?php echo $step->step_title ?>" name="step_title" class="uk-width-90 uk-form-large"
type="text" placeholder="step title">
<?php echo form_error('step_title'); ?>
</div>
<div class="uk-margin-bottom uk-form-controls">
<textarea name="step_description" rows="4" class="uk-width-90 uk-form-large ckeditor"
placeholder="Enter Description"><?php echo set_value('step_description', $step->step_description,
true) ?></textarea>
<?php echo form_error('step_description'); ?>
</div>
<a href="<?php echo site_url('articles/delete/' . $step->step_id) ?>"
class="uk-button uk-button-danger uk-float-left">Delete</a>
<button class="uk-button uk-button-success uk-float-right">Save</button>
</div>
</form>
This is a hard problem to guess at with what you have, though it sounds like a mistake I made when I first started working with PHP and databases. That is converting output in value fields using htmlspecialchars($value,ENT_QUOTES). If you have this value:
$content = '"What are you talking about?" she asked, "that is ridiculous!"';
then you echo into an input field for editing:
<input name="test" value="<?php echo $content ?>" />
The html output is:
<input name="test" value=""What are you talking about?" she asked, "that is ridiculous!"" />
which has two sets of quotes, and most browsers then will show that field as blank. Then when you click the update, it clears out what you previously had. To get around that, you should use:
<input name="test" value="<?php echo htmspecialchars($content,ENT_QUOTES) ?>" />
which then gives you this output in the field:
"What are you talking about?" she asked, "that is ridiculous!"
but the html looks like this:
<input name="test" value=""What are you talking about?" she asked, "that is rediculous!"" />
This is just a stab in the dark based on your description and based on personal experience. You will have to show your html or some of the php values for a more clear picture.
I have following code on the top of product detail page
<?php
include("include/connection.php");
session_start();
$ses_id = session_id();
$ID = (int)$_REQUEST['id'];
if (!isset ($_POST['submit']))
{
?>
<div class="row">
<div class="span9">
<div class="span6 ">
<h4>Review(0)</h4>
<p>There are no review for this product</p>
<h4>Write a Review</h4>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>Your Name:</p>
<input type="text" id="txtname" name="Rname" placeholder="write your name..." />
<p>Your Review</p>
Excelent<input type="radio" name="reviewbtn" class="radio" value="Excelent" />
Good<input type="radio" class="radio" name="reviewbtn" value="Good" />
Poor<input type="radio" class="radio" name="reviewbtn" value="Poor" /><br/>
<textarea id="txtreview" name="txtbx" cols="50" rows="10"
class="container-fluid"></textarea>
<input type="submit" value="submit" class="btn" />
</form>
</div>
</div>
</div>
<?php
} else {
$Rname = mysql_real_escape_string(stripslashes(htmlentities($_POST["Rname"])));
$Reviewbtn = $_POST["reviewbtn"];
$Txtbox= mysql_real_escape_string(stripslashes(htmlentities($_POST["txtbx"])));
$sql=mysql_query ("INSERT INTO reviews (Name, Comments,Rating) VALUES('$Rname','$Reviewbtn','$Txtbox')", $con) or die (mysql_error());
echo "Record added succesfully. You will be redirected to previous page in 5 seconds";
header( "refresh:5;url= product_detail.php" );
}
mysql_close($con);
?>
here I am getting error
Notice: Undefined index: id in \product_detail.php on line 5
( which is $ID = (int)$_REQUEST['id']; )
basically I am building a rating form on a product,please if someone can modify the code so form submission could be done using ajax jquery call so page should not load again
remove int from it there no need of it.
$ID = $_REQUEST['id'];
There is no Element named ID in your form
So when you are going to catch this it occurred error
$ID = $_REQUEST['id'];
^ provide actual form element's name here
When you coming to this page from previous page it is not showing error
after submitting from it is showing error.
If it is, add an hidden element into your form
<input type='hidden' name='id' value="<?php echo $ID; ?>">
It will solve the problem
Use some other word as name attribute rather than naming it as id you can set it to user_id or something else you want,because it is not a good practice
if(isset($_REQUEST['user_id'])){
$ID = $_REQUEST['user_id'];
}
Ok here is my problem, and all of my 'dirty' code. This isn't my production code but just trying to make it work at the moment.
Basically what I need is when a user selects the Not Ok radio button it displays the textarea for that unique set which it doesn't do right now when I select Not Ok it gives the textarea's for all the entries which right now is about 13 sets of questions that get generated dynamically from a mysql database at the moment. I have a feeling it has to do something with unique id's that are either in the wrong place in my code now, or just simply aren't there at all. Any help is appreciated greatly.
<div data-role="collapsible" data-theme="b" data-content-theme="c">
<h3>Vehicle Check Information</h3>
<?php
$query = mysql_query("SELECT * FROM vehicle_q");
while($row = mysql_fetch_array($query)) {
$q_title = $row['title'];
$q_id = $row['id'];
?>
<div data-role="fieldcontain" style="border:0;">
<fieldset data-role="controlgroup">
<legend><?php echo $q_title; ?>:</legend>
<input type="radio" name="help[]" id="checkbox-1a" value="Ok" />
<label for="checkbox-1a">Ok</label>
<input type="radio" name="help[]" id="checkbox-2a" value="Not Ok" />
<label for="checkbox-2a">Not Ok</label>
</fieldset>
<div id="hidden_text-<?php echo $q_id; ?>" style="display:none;">
<script>
$(document).ready(function(){
$(":radio:eq(1)").click(function(){
$("#hidden_text-<?php echo $q_id; ?>").show(500);
});
$(":radio:eq(0)").click(function(){
$("#hidden_text-<?php echo $q_id; ?>").hide(500);
});
});
</script>
<fieldset data-role="fieldcontain">
<label for="<?php echo $q_title; ?>_t">Explain the Deficiency(If any):</label>
<textarea name="text_a[]" id="<?php echo $q_title; ?>_t"></textarea>
</fieldset>
</div>
</div>
<input type="hidden" name="q_title1[]" value="<?php echo $q_title; ?>" />
<?php
}
?>
</div>
The other 2 answers are ways to fix your issue. As a side, here is possibly why it is happening. When you are using -
$(":radio:eq(1)").click(function()
$(":radio:eq(0)").click(function()
You are using a click listener that just checks for if 'any' radio button with that index position was clicked on your page. So any button with 1 index position will make every $(":radio:eq(1)").click(function() execute.
Edit -
You would want to change your radio button ids, as (1) they will not be unique as you repeat them with each while() loop, and (2) you could use it to check if that specific radio buttton was clicked.
Try changing it to -
...
<input type="radio" name="help[]" id="checkbox-1a-<?php echo $q_id; ?>" value="Ok" />
<label for="checkbox-1a">Ok</label>
<input type="radio" name="help[]" id="checkbox-2a-<?php echo $q_id; ?>" value="Not Ok" />
<label for="checkbox-2a">Not Ok</label>
</fieldset>
<div id="hidden_text-<?php echo $q_id; ?>" style="display:none;">
<script>
$(document).ready(function(){
$("#checkbox-2a-<?php echo $q_id; ?>").click(function(){
$("#hidden_text-<?php echo $q_id; ?>").show(500);
});
$("#checkbox-1a-<?php echo $q_id; ?>").click(function(){
$("#hidden_text-<?php echo $q_id; ?>").hide(500);
});
});
</script>
The easiest way that I have found to show hide elements based off of radio values is to serve up an onchange event based on the id of the element (which you already have). In your case, since you only have two radios based off the same name, the easiest way is a simple if/else statement. Something like:
<script type="text/javascript">
$(function(){
$('#checkbox-1a').change(function(){
if($(this).attr('checked')){
$('#hidden-text-<?php echo $q_id;?>').hide(500);
}else{
$('#hidden-text-<?php echo $q_id;?>').show(500);
}
});
});
</script>
Where we state when #checkbox-1a is changed: if the element is checked (selected), then do something (in your case hide something), otherwise do something else (in your case show something).
Try something like:
<div data-role="collapsible" data-theme="b" data-content-theme="c">
<h3>Vehicle Check Information</h3>
<?php
$query = mysql_query("SELECT id,title FROM vehicle_q");
while($row = mysql_fetch_array($query)) {
$q_title = $row['title'];
$q_id = $row['id'];
?>
<div data-role="fieldcontain" style="border:0;" class="groupings">
<fieldset data-role="controlgroup">
<legend><?=$q_title?>:</legend>
<input type="radio" name="help[]" id="checkbox-<?=$q_id?>-1a" value="Ok" />
<label for="checkbox-<?=$q_id?>-1a">Ok</label>
<input type="radio" name="help[]" id="checkbox-<?=$q_id?>-2a" value="Not Ok" />
<label for="checkbox-<?=$q_id?>-2a">Not Ok</label>
</fieldset>
<div id="hidden_text-<?=$q_id?>" class="hiddenText" style="display:none;">
<fieldset data-role="fieldcontain">
<label for="<?=$q_title?>_t">Explain the Deficiency(If any):</label>
<textarea name="text_<?=$q_id?>_a[]" id="<?=$q_title?>_t"></textarea>
</fieldset>
</div>
</div>
<input type="hidden" name="q_title1[]" value="<?php echo $q_title; ?>" />
<?php
}
?>
</div>
<script>
$(document).ready(function(){
$(".groupings input:radio").click(function(){
if (this.value == "Ok") {
$(this).parent().next('.hiddenText').show(500);
} else {
$(this).parent().next('.hiddenText').hide(500);
}
});
});
</script>
This is untested, but the script only needs to be declared once and it will apply to the rest relative to the radio button.
I want to change an input value depending on a $_POST variable. Something like this:
<?php if ($_POST['yourname'] != "")
{
$('input[name=yourname]').attr('title', $_POST['yourname']);
} ?>
But it doesn't work. How can I do that?
Thanks!
EDIT: I have this input:
<input class="clearme" name="yourname" type="text" title="Insert your name" />
Add directly to html:
<input class="clearme" name="yourname" type="text" title="Insert your name" value="<?php echo isset($_POST['yourname']) ? $_POST['yourname'] : ''; ?>" />
You can't use javascript in PHP. PHP runs on the server, while javascript runs in the clients browser.
You need to do this in pure javascript, not PHP.
Try this in the one file, replacing 'name_of_cur_file.php' with the name of the current php file.:
<?php
if($_POST['yourname'] > ""){
$yourname = $_POST['yourname'];
}else{
$yourname = ""; // set to avoid errors
}
?>
<form action="name_of_cur_file.php" method="post">
<input type="text" value="<?php echo $yourname;?>" />
<input type="submit" value="go" />
</form>
WARNING!!! - Lacking any validation for this example!!!
If you want the code sent from the server to execute then:
<?php if ($_POST['yourname'] != "")
{
<script type="text/javascript">
$(document).ready(function () {
$('input[name=yourname]').attr('title', $_POST['yourname']);
});
</script>
} ?>
You have jquery code in php.You have complete first php tag then you have write your jquery code in script code like
<?php if ($_POST['yourname'] != "")
{
?>
<script>
$('input[name=yourname]').attr('title', $_POST['yourname']);
</script>
<?php
} ?>
You can also write php code only like
<?php
$title="Insert your name";
if ($_POST['yourname'] != "")
{
$title=$_POST['yourname'];
} ?>
<input class="clearme" name="yourname" type="text" title="<?php echo $title; ?>" />
Hello
I am building a form in a mvc system view, and i want that all the inserted values will be kept,in case of form submit failure.
How can this be done: i tried like (example for a field):
<label for="user_firstname">Nume</label>
<input id="user_firstname" type="text" name="user_firstname" value=<?= $_POST['user_firstmane'] ?> >
<? if (isset($errors['user_firstname'])): ?>
<span class="error"><?= $errors['user_firstname']; ?></span>
<? endif; ?>
but of course, it doesn't work the first time (when no post action is done).
what is the simplest way to do this? any ideas?
thank you
Just loop through the DOM in javascript and put the PHP $_POST data into the input.value
<script type='text/javascript'>
<?php
echo "var jsArray = new Array();";
foreach ($_POST as $key=>$value){
echo "jsArray['$key'] = '$value';"; //turn it into a javascript array
}
?>
// Grab all elements that have tagname input
var inputArr = document.getElementsByTagName("input");
// Loop through those elements and fill in data
for (var i = 0; i < inputArr.length; i++){
inputArr[i].value = jsArray[inputArr[i].name];
}
</script>
I would suggest something like:
<label for="user_firstname">Nume</label>
<input id="user_firstname" type="text" name="user_firstname" value=<?(isset($_POST['user_firstname']) ? $_POST['user_firstname'] : ""; ?>>
<? if (isset($errors['user_firstname'])): ?>
<span class="error"><?= $errors['user_firstname']; ?></span>
<? endif; ?>
You also had a typo in the $_POST["user_firstmane"] should be $_POST["user_firstname"] :)
value="<?php echo isset($_POST['user_firstname'])? $_POST['user_firstname'] : "" ?>"
You mean you want to keep the value of the form when it failed to submit? You can use $_SESSION to store the value in the check page. For example:
check.php
<?php
session_start();
if (strlen($_POST['user_firstname']) < 5) { //for example
$_SESSION['user_firstname'] = $_POST['user_firstname'];
}
?>
In your current form. change value=<?= $_POST['user_firstmane'] ?> to value="<?=$_SESSION['user_firstname']?>", so:
<label for="user_firstname">Nume</label>
<input id="user_firstname" type="text" name="user_firstname" value="<?=$_SESSION['user_firstname']?>" />
<? if (isset($errors['user_firstname'])): ?>
<span class="error"><?= $errors['user_firstname']; ?></span>
<? endif; ?>
<input id="FirstName" name="FirstName" placeholder="First name" title="First Name" required="" tabindex="1" type="text" value="<?php if(isset($_POST['FirstName'])){ echo htmlentities($_POST['FirstName']);}?>"/>
This code is much more easier to keep form info after submit form failed.