I'm a total newbie and want to start with php. I know some javascript already.
I want to be able to type some text in a form and convert it to a query e.g.
In my website there's this search box, I type in 'example' click submit and it gives me this=
http://www.externalsite.com/search?s=example&x=0
and pastes it to the address bar, you know like a search engine.
Any guidance will be appreciated.
Well, as you are doing PHP, you should point your form to submit to a PHP file. Then to retrieve the data, use $_GET or $_POST depending your form is posting or getting (as I can see from your exemple its a GET) so something like this :
HTML :
<form method="get" action="search.php">
<input type="text" name="q" id="q" value="" />
<input type="submit" value="Submit" />
</form>
On the PHP side :
<?php
$query = $_GET['q'];
header('Location: google.com/search?q=' . $query . '%20term');
die();
?>
Basically you're typing your search term into a form, which then posts (via GET) to a search page, which queries its database for records matching that string. A simple example of this follows:
index.php
<form method="get" action="search.php">
<p><input type="text" name="terms" /></p>
<p><input type="submit" value="Search" /></p>
</form>
When you submit that, it will direct you to search.php?terms=[terms here]. Our code found within search.php follows:
search.php
mysql_connect($host, $user, $pass) or die(mysql_error());
$terms = $_GET["terms"]; // you'll want to sanitize this data before using
$query = "SELECT col1, col2, col3
FROM tablename
WHERE col1 LIKE '%{$terms}%'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
print "We've found results.";
} else {
print "No results found.";
}
This is a very simple example (don't copy/paste this into production). Essentially you're pulling the submitted value(s) into a query, and then showing any results. This should be enough to get you started, but feel free to visit us here if/when you have more specific questions in the future.
Best of luck!
Related
If I use a String for the $key my code works but if I use $hasorhasnotentered it returns "Array". How can I use a Variable for the $key?
<?php
$user_id = $current_user->ID;
$key = $hasorhasnotentered;
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>';
?>
The Variable is being defined by user interaction of a form below is the form code
<form method="post" id="adduser" action="/my-likes-list/">
<input type="text" name="<?php echo $hasorhasnotentered;?>" id="<?php echo $hasorhasnotentered;?>" value="<?php echo $yesentered; ?>"/>
<p class="form-submit">
<input name="updateuser" type="submit" id="updateuser" class="submit button" value="update-user" onclick='enterdnew()'/>
<?php wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="Update" />
</p>
</form>
Your code looks OK, but I expect your $hasorhasnotentered variable is not undefined. If it's value comes from user input check for definedness before you use it!
You can use a variable just as you did. It's just that the variable needs to contain a string. Evidently, your $hasorhasnotentered contains. That explains why it would return an array because if $key is left blank get_user_meta returns all of the meta data in an array. See the documentation: https://codex.wordpress.org/Function_Reference/get_user_meta
$key
(string) (optional) The meta_key in the wp_usermeta table for the meta_value to be returned. If left empty, will return all user_meta fields for the given user.
Default: (empty string)
Edit:
In the form code above, I believe you want:
<input type="text" name="hasorhasnotentered" id="hasorhasnotentered" value="<?php echo $hasorhasnotentered; ?>"/>
Then, on load, the value of the element will be set to whatever $hasorhasnotentered is. And the user will set $hasorhasnotentered to be whatever text they type in the element.
Then, on submit, $hasorhasnotentered will be the text of the input field.
I have to admit, I may not be understanding the intent of your code.
I am customizing a Shopify template has a form like this.
<form action="/search" method="get" class="search-bar" role="search">
<input type="hidden" name="type" value="product">
<input type="search" name="q" class="text" placeholder="{{ 'general.search.placeholder' | t }}" value="{{ search.terms }}">
<input type="hidden" class="btn" value="Search">
</form>
Which returns an array of objects search.results it is not possible to remove elements from that array in Liquid (ex. remove products which has a price of 0)
I want to remove elements from that array because even though i can filter those elements and choose them to show in the page or not, i cant effect {% paginate %} function because it is paginating the unfiltered version of search.results for example {% paginate search.results by 12 %}.
So my question is can i send an advanced query from the very start and only get the result for ex. products which have not a price of 0 ?
Thanks in advance.
I googled about this a lot but couldn't find a solution.
`
Nope you can't. Price is not a valid field in Shopify's search fields - https://help.shopify.com/manual/sell-online/online-store/storefront-search
Alternatively you can tag the products with 0 price and add "-tag" as a field to exclude those items. Refer to the link for detailed description on search on Shopify.
Basically, I have made a form which allows you to input 2 numbers, and when you press the 'Add' button, the program writes the answer onto the screen, the only thing is when the answer is written, it appears on a separate page. How do I get it to write to the same page, below is the HTML code:
<form type="twoNum" method="get">
<input type="float" placeholder="Enter first number here..." name="num1" id="n1"/>
<input type="float" placeholder="Enter second number here..." name="num2" id="n2"/>
<input type="button" value="Add" name="sndfunct" onClick="twoNum(this.form);"/>
</form>
Below is the Javascript code:
function twoNum(form)
{
var num1 = form.num1.value;
var num2 = form.num2.value;
var intNum1 = parseFloat(num1);
var intNum2 = parseFloat(num2);
document.writeln(intNum1 + intNum2);
}
Please note that using document.write() is considered bad practice. E.g. see the warning on the W3C web site.
Furthermore, you can’t use it to edit a closed document. document.write() can only be used while the document is being loaded.
In order to do what you want, you should have a <span id="foo"></span> somewhere in your document, and then do:
document.getElementById("foo").textContent = intNum1 + intNum2;
This will insert your number inside your span element. Actually, it replaces the content of the (previously empty) span element.
Edit: Of course, it can be any kind of element. I used a span element just for the example.
You can write <p id="answer"></p> in the form. Then create var answer=intNum1 + intNum2 and after that instead document.writeln(intNum1 + intNum2) write document.getElementById("answer").innerHTML=answer!
I want to use Excel 2010 to do calculations from my personal website. I would like for the user to enter data in a field and press submit. Then, Excel takes the data, performs calculations on it, and outputs the calculated data into another field form that the user can see.
This needs to be done away from the actual website because I don't want to share the formulas on Excel.
For example:
A web page has three fields: First digit, second digit, and solution. The end user enters 2 and 5 into the first and second forms and Press submit. An Excel sheet adds these numbers in the background. The number 7 appears in the solution field.
What is the best way to do this?
You say this is on your personal website, so I assume you have access to the server and can do some rudimentary scripting. Is it absolutely necessary to use Excel? This seems like it would be more straightforward to employ perl, PHP, python, etc. to perform simple addition.
If you did the calculations in Javascript, you would be exposing the formulas to viewers, as the client executes the script and therefore must have the source code.
However, with one of the aforementioned scripting languages, the source of the script is parsed by the server but never passed to the user.
For example, a php script could simply send the results of a form submit to itself:
Areas between <?php and ?> are parsed server-side and never passed to the user.
<?php
$a = '';
$b = '';
$result = '';
if (#$_POST['submit'] == 'Submit')
{
$a = $_POST['a'];
$b = $_POST['b'];
$result = SecretFormula($a, $b);
}
function SecretFormula($a, $b)
{
return $a + $b;
}
?>
<html>
<head>
</head>
<body>
<form id="form" method="post" action="">
Enter Number A: <input name="a" type="text" value="<?php echo $a; ?>" /><br />
Enter Number B: <input name="b" type="text" value="<?php echo $b; ?>" /><br />
<input name="submit" type="submit" value="Submit" /><br />
<br />
Result: <input name="result" type="text" value="<?php echo $result; ?>" />
</form>
</body>
</html>
I am learning modx and stuck at a point. I want to post "uid" value to next page via POST only and want to set hidden field which will contain "uid" as a value. I believe practically its not allowed to pass values to chunk.
I want to know whats the proper way so i can get POST data and use that value in chunk ??
My procedure
1) I have created resource (document) which contains call to snippet and then chunk
2) snippet contains value retrieved from POST
3) chunk contains a form and fields, I want to inject that POST value in this form.
There are several ways you could do this.
1) Return the uid value directly from the snippet (let's call it getPostData), and place the snippet call in your hidden field in the chunk like this:
<input type="hidden" name="uid" value="[[!getPostData]]" />
Note the snippet is uncached ([[! opening tag) otherwise the first form submission will be cached.
2) Place the snippet call in the chunk tag and have the value passed into a placeholder:
[[$myChunk?uid=`[[!getPostData]]`]]
...and in your chunk set an uncached placeholder for 'uid':
<input type="hidden" name="uid" value="[[!+uid]]" />
3) Recommended: Use setPlaceholders() in your snippet to output content to placeholders anywhere in your page - you can use it to output to multiple placeholders:
<?php
// please sanitise your POST values, this is just an example
$placeholders = array();
$placeholders['uid'] = $_POST['uid'];
$placeholders['email'] = $_POST['email'];
$modx->setPlaceholders($placeholders);
...and your chunk:
<input type="hidden" name="uid" value="[[!+uid]]" />
<input type="email" name="email" value="[[!+email]]" />
Documentation: http://rtfm.modx.com/display/revolution20/modX.setPlaceholders