Hide html value inside input field - php

I would like to know if it is possible to help me please.
It's a live ajax search that retrieves information in the database. This is the php code :
<?php
$key=$_GET['key'];
$array = array();
$connection=mysqli_connect("localhost","root","","visitor_signin_app");
$query = mysqli_query($connection, "SELECT * FROM visitors WHERE visitor_first_name LIKE '%{$key}%' AND visitor_visit_status = 'Signed Out'");
while($row = mysqli_fetch_assoc($query)) {
$array[] = '<span style="display:none; visibility:hidden">'.$row['visitor_id'].'</span>' . ' ' . $row['visitor_first_name'] . ' ' . $row['visitor_last_name'];
}
echo json_encode($array);
mysqli_close($connection);
?>
In the array I am trying to hide the 'visitor_id' and when I start typing the name in the input field it works perfect where it only shows the first name and last name, but once I select the name that displays in the dropdown then it inserts the
<span style="display:none; visibility:hidden">1</span>
So my main question is, would it be possible to hide the html that I dont want to display in the input field but the value needs to be added with the name that gets chosen. Any advice would be gladly appreciated. Thank you
This is the link where I received the code : https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
I would like to retrieve all the information from the registered person that gets selected in the dropdown list and add them as a new entry to the database.

Why not use the HTML inputwith type hidden ? See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
I think it's made exactly for your purpose.

Your question is a bit confusing to understand, but if I'm correct, what you want to do is have a hidden input field, but set the value of the input field based on the value of a different input field.
To do that, you should give your hidden field a display: none style, and use JavaScript to set it's value. Something like this:
document.getElementById('inputId').value = 'your data';

Related

Allow PHP in Contact Form 7 editor

I need a form to be able to get dynamic date values from php
My current form looks like the following:
[radio date default:1 "1" "2017-11-16" "2017-11-17" "2017-11-18" "2017-11-20" "2017-11-21"]
Is there any way to use php values instead?
Ideally, I would be able to put something like this in the editor itself
[radio date default:1 "1" "<?php echo date('Y-m-d'); ?>"]
Is this possible?
you can make your own radio button shortcodes with different names you shoud go on this way. not to run php on editor.
you can use this hook
wpcf7_add_shortcode('date_radio_button', 'wpcf7_date_radio_button');
function wpcf7_date_radio_button($tag) {
if (!is_array($tag)) return '';
$name = $tag['name'];
if (empty($name)) return '';
$html = '<input type="radio" name="' . $name . '" value="' . date('Y-m-d') . '" />';
return $html;
}
Here is the simple way to change the value of radio button,
What you need to do is, give an ID to radio button,
For example,
[radio field_name id:date_radio default:1 "1" "2" "3" "4"]
Where I have given ID "date_radio".
Now, in your page, you need to add following jQuery code:
<script>
jQuery(document).ready(function(){
jQuery('#date_radio').find("input[type='radio']").attr('value','<?php echo date('Y-m-d');?>'); // if you are going to put this code in .js file, then you need to get value from hidden field where you need to give date as value.
});
</script>
Make sure, Above code will change the value of all the radio option for given ID.
Please let me know if you need any further help in this.
Thanks!

Need a PHP echo to produce a text hyperlink on buffer

I have a php form on my site. The form ultimately produces an article submitted by the user.
One of the data fields in the form, is a drop down menu so the user can select which publication they represent.
Currently, the form works if I am only trying to display the name of their publication.
However... I would also like that name to be a clickable link to their respective publication.
In an attempt to achieve this, I set my form up like this:
Form: <select name="publication" id="publication">
<option value="http://www.espn.com">ESPN</option>
<option value="http://www.cnn.com">CNN</option>
<option value="http://www.abcnews.com">ABC</option>
<option value="http://www.cbsnews.com">CBS</option>
<option value="http://www.foxnews.com">FOX</option>
</select>
And the echo is set up like this:
Echo:
<?php $publication = htmlspecialchars($_POST['publication']); echo $publication; ?>
Unfortunately, the result produces the full URL instead of the Text Link I am trying to achieve.
Not sure how I am supposed to code the form or the echo to achieve the desired clickable text link.
It is taking the value option, not the innerHTML. One solution is to make the value the following:
<a href='http://example.org'>Example</a>
Note: You may need to escape characters for this to work.
The value of $_POST['publication'] is going to be the chosen <option>'s value, not the display name.
If you want to display the link along with the name, consider encoding the list of publications into a static array, then outputting that:
$publications = [
"media1" => "link",
"media2" => "link"
]
if (array_key_exists($_POST["publication"], $publications)) {
echo '' . htmlspecialchars($_POST["publication"]) . '';
}

SQL Query onsubmit - change existing results

Good morning everyone,
I have a page that performs a default connection to database, search and looped output of data to the page by applying a template for each result in the table using this code:
<?php
$result = mysqli_query($con,"SELECT * FROM XXX WHERE Type='ABCD';");
while($row = mysqli_fetch_array($result))
{
include('includes/template-1234.php');
}
mysqli_close($con);
?>
On the same page I have a simple HTML search box:
<div id="srch-form">
<form>
<input name="srch-box" type="text" id="srch-box" size="45" onClick="this.value='';" onFocus="this.select()" onBlur="this.value=!this.value?'Enter Product Name or SKU#':this.value;" value="Enter Product Name or SKU#">
<input type="submit" name="srch-btn" id="srch-btn" value="Search">
</form>
</div>
Because I am just learning PHP - how do I set this up so that the default action still occurs on page entry, but - onsubmit of something in the searchbox the data in the current page is replaced by the results of the submission? I guess I'm getting confused over the fact that since I have already assigned a value(s) to $result to get the default output, If the value of $result is changed by the search form, does PHP automatically refresh the page when the value of $result is changed? - Sorry if this is a simple thing.
First, you'll have to add an action (which file to refer when submitted) and preferably a method-type in your form-tag. You will also have to include value of $productName (given from server-side php) inside of your html (look at value attribute)
Do something like this:
html-form: (Examplefile: yourhtmlform.php)
Then in your php-file get value from a specific element like this:
<?php
//File querydb.php (example filename)
//Form is submitted and srch-box is set (only set when form is submitted)
if(isset($_POST['srch-box'])) {
$type = $_POST['srch-box']; //Get value of input-element
}
else {
$type = 'kingkong'; //Default type to search
}
$prepareQuery = mysqli_prepare($dbConnectionLink,"SELECT * FROM XXX WHERE Type=:type");
//...code to fetch results..
//You get a productName (or SKU) from db
if (isset($result['product_name'])) {
$productName = $result['product_name']; //or whatever your column is called
}
else {
$productName = '';
}
include('yourhtmlform.php'); //Observe that it must be php to echo out value of $productName in this included file
I noticed you didn't use prepared-statement. Look for more information on prepared statements here: http://www.php.net/manual/en/mysqli.prepare.php (use them!)
I hope this will help you somehow :-)

Dynamically create and repeat the set of fields

I have a PHP form which shows one 'User details' block by default. 'User details' section has first name, last name and address etc. I have a requirement to repeat the same block after clicking on the button so the user can enter 'n' number of users into on the form. Also I need to save all the information in MySQL database. What is the best way to achieve this? Please let me know. I appreciate any help.
Thank you.
I would use jQuery to add additional form elements. You'll need to give each element a different 'name' attribute though- I'd use something like name='user1name' (and in the next username field name='user2name'). Comment on my post if you need specific help with that. When the form submits, I'd use php to loop through each set of fields with an insert statement at the end of the loop.
PHP is something like this:
for ($i = 1; is_null($_POST['name' . $i]) == false; $i++) {
$name = $_POST['user' . $i . 'name'];
// repeat for all your fields
// do your insert statement here - 1 insert per user created
mysql_query("INSERT INTO table_name (Name, Address, ...) VALUES ('$name', '$address', ...)");
}
jQuery is something like this:
Existing HTML
Should only have 1 div with class=user at this point
<div id='fields'>
<div class='user'>
<input type='text' name='user1name' />
<input type='text' name='user2name' />
... more elements ...
</div>
</div>
jQuery
var userFields = $("div.user").clone();
$(userFields).appendTo("div#fields");
$("div#fields").children("div.user:last").children().each(function() {
// change names here!
});
This will copy the existing field and add its copy to the fields div. It will also set the new names for the second set of user fields. Note: you only need ot set userFields once, and then you can use $(userFields).appendTo("div#fields"); as many times as you need to (as long as the userFields variable is in scope).
If you have event handlers attached to your elements in the user field, use $("div.user").clone(true, true); instead of $("div.user").clone();.

Auto filling --Dropbox

i have two text type forms(id,name) and another form dropbox type(school name)(select option type).
when user enters the id form, the remaining form (name) and the drop box(school name) needs to be filled based on the values in mysql.
filling form text type (name) is not a problem which i am able to do using json and jquery ,but i do not know how to auto select the dropbox.
any tutorials or code snippets suggestions are appreciated.
if your drop box is in xhtml, you can use the "selected" param.
with jquery, you could write semething like :
$("#optiontoSelect").attr("selected","selected");
More info in the jquery doc
I may have misunderstood the question. If you wish the focus to move from the input field to the select you would use .focus(). I would imagine you want to call focus on successful population of the select.
$('#select').focus();
or
$(this).focus();
jQuery Docs : focus()
This'll get you started:
<?php
$options = Array('red', 'green', 'blue');
$opt_from_db = 'green'; // for example
echo '<select name="fieldName">';
foreach ($options as $opt) {
$selected = ($opt_from_db == $opt) ? ' selected' : '';
echo "<option{$selected}>{$opt}</option>";
}
echo '</select>';
?>
The key is to write the selected attribute (in some form or another) into the programmatically-rendered HTML.

Categories