How to get content (a specified content) from inside a #div.
I have form like:
<?php
require_once('phpQuery/phpQuery.php');
$key-one = $_POST['key01'];
$html = file_get_contents('http://google.com/search?q='.$key-one.'');
phpQuery::newDocumentHTML($html);
$result01 = pq('div#resultStats');
?>
<body>
<form name="form1" method="post" action="1.php">
<input type="text" name="key01"><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
I got result like:
About 1,570,000,000 results
But I only want
1,570,000,000
Thanks in advance.
You can use PHP str_replace.
Assuming $result01 displays About 1,570,000,000 results:
$result01 = str_replace("About ","",$result01);
$result01 = str_replace(" results","",$result01);
After these changes, echo $result01 will display 1,570,000,000.
you can also use preg_replace,like
$result = preg_replace('/[^\sa-z]/i', '', $string);
this removes every characters and whitespace from your string
Related
I have three text boxes and I want to place a value in textbox 1 and 2,then add them together and get the result in textbox 3.With the below code I can get it to echo out onto the screen,but If I omit the echo then nothing happens after placing numbers in T1 andT2.
Can anybody tell me what I am doing wrong.
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="textentry2.php" method="POST">
<input type = "submit" name = "submit" value = "go">
<input type = "text" name = "text1" >
<input type = "text" name = "text2" >
<input type = "text" name = "text3" >
<?php
$text_entry = $_POST['text1'];
$text_entry2 = $_POST['text2'];
$text_entry3 = $_POST['text3'];
{
$text_entry3 = ($text_entry + $text_entry2);
echo ($text_entry3);
}
?>
nothing happens after placing numbers in T1 andT2
PHP is a server-side language. you can't expect from PHP to act real-time...
input values need to go to server, then PHP Server can calculate your values on server then after refresh, results comes...
if you want to write a real-time calculator, you need to write this with javaScript. absolutely you can do that with AJAX or anything else but javaScript is a easy and fast way for this.
try this:
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="textentry2.php" method="POST">
<input type="submit" name="submit" value = "go">
<input type="text" name="text1" onkeyup="calc()">
<input type="text" name="text2" onkeyup="calc()">
<input type="text" name="text3" >
</form>
<script>
function calc()
{
var elm = document.forms["myform"];
if (elm["text1"].value != "" && elm["text2"].value != "")
{elm["text3"].value = parseInt(elm["text1"].value) + parseInt(elm["text2"].value);}
}
</script>
</body>
</html>
For Strings:
$text_entry3 = ($text_entry.$text_entry2);
in php you combine strings with a . between them.
For Integer:
$text_entry3 = ((int)$text_entry + (int)$text_entry2);
You need to cast them as you get Strings from your from and they won't add together that easy without casting.
For Placing it:
as Aravona stated allready:
You should make all the calculation befor you output the html. Php is dynamic. HTML not. Thus you need to put the dynamic part into the static one
Make the <?php ... ?> in the beginnin and than use this to output the result:
<input type = "text" name = "text3" value="<?=$text_entry3;?>" >
I think your code would be like this:
<?php
$text_entry1 = (int)$_POST['text1'];
$text_entry2 = (int)$_POST['text2'];
$text_entry3 = ($text_entry1 + $text_entry2);
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="textentry2.php" method="POST">
<input type = "submit" name = "submit" value = "go">
<input type = "text" name = "text1" value = "<?php echo $text_entry1 ?>" >
<input type = "text" name = "text2" value = "<?php echo $text_entry2 ?>" >
<input type = "text" name = "text3" value = "<?php echo $text_entry3 ?>" >
</form>
</body>
</html>
Firstly put your PHP above your HTML so the values are defined.
Then:
<input type = "text" name = "text3"
value="<?php echo ((int)$text_entry + (int)$text_entry2); ?>">
A very good tips I tend to give those who are new to PHP, is to put all of your PHP code before any HTML. This means that you'll take care of all of your data processing, before you try to send the client anything. The beauty of that, is that if some erroneous condition appears, you can simply redirect or do whatever's necessary to provide the user with a clear and informative responce. Instead of just dumping a half-created page, with an ugly error (or several) in the middle of it.
Thus your PHP code should look something like this:
<?php
// First make sure that the variable is defined, to prevent notices
// when trying to use it later.
$answer = '';
// If something has been posted (submitted).
if (!empty ($_POST)) {
// Calculate the answer.
// TODO: Add some validation to make sure that the values are indeed numbers.
// See filter_var () in the PHP manual.
$answer = $_POST['text1'] + $_POST['text2'];
}
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myform" action="textentry2.php" method="POST">
<input type="submit" name="submit" value = "go">
<input type="number" name="text1" >
<input type="number" name="text2" >
<input type="number" name="text3" value="<?php echo $answer; ?>">
</form>
Normally I'd use htmlspecialchars() on $answer when echoing it, to prevent XSS attacks. However, since it only echo out a number it is not necessary in this case.
I have a data base 'School'. It has only one table - 'Words'. There are word_id, word_name, word_description in it. I want to pull a random description and display it on a page. Then I want to input a word and see if the word has the same description as the random one that was pulled. What am I doing wrong? Here is the code -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Изпит</title>
</head>
<body>
<?php
$connection = mysqli_connect('localhost', 'root', '', 'school');
if(!$connection){
echo 'NOT OK';
exit;
}
if(isset($_POST['submit_description'])){
$q = mysqli_query($connection, ' SELECT word_description
FROM words ORDER BY rand() LIMIT 1
');
$row=mysqli_fetch_assoc($q);
if($row){
$_POST['word_description'] = $row['word_description'];
echo $_POST['word_description'];
}
}
if(isset($_POST['submit_word'])){
$word_name = $_POST['word_name'];
$q2="SELECT * FROM words WHERE word_name='$word_name' and word_description='".$_POST['word_decsription']."'";
$result=mysqli_query($connection, $q2);
$count=mysqli_num_rows($result);
if($count==1){
echo 'Позна ве.';
}else{
echo 'Не позна ве.';
}
}
?>
<br><br><br>
<form method="POST">
<input type="submit" name="submit_description" value="Искай описание.">
<input type="hidden" name="word_description" value="<?php echo $_POST['word_description']?>">
</form>
<form method="POST">
<input type="text" name="word_name">
<input type="submit" name="submit_word" value="Провери дума.">
</form>
</body>
</html>
I think you have some typos.
This line of code here:
$q2="SELECT * FROM words WHERE word_name='$word_name' and word_description='".$_POST['word_decsription']."'";
Should be like this:
$q2="SELECT * FROM words WHERE word_name='".$word_name."' and word_description='".$_POST['word_description']."'";
1) There is a typo in $_POST['word_description'] in your query:
$q2="SELECT * FROM words WHERE word_name='$word_name' and word_description='".$_POST['word_decsription']."'";
2) Also, I would recommend using the word_id instead of the word description to make the verification... you would need to write it in a <input name="word_id" type="hidden" value="..." /> in your form to pass it along.
What would be even better, to prevent people from knowing the answer by looking at the code (in case they would know what word matches what id), you could encode the value in the hidden field to be md5($word_id.$word_name) and then in your query you check "WHERE MD5(CONCAT(word_id, word_name))='".$_POST['word_md5']."'" (assuming your hidden input is now called "word_md5).
EDIT:
After looking at the HTML I see what your problem is:
<form method="POST">
<input type="submit" name="submit_description" value="Искай описание.">
<input type="hidden" name="word_description" value="<?php echo $_POST['word_description']?>">
</form>
<form method="POST">
<input type="text" name="word_name">
<input type="submit" name="submit_word" value="Провери дума.">
</form>
This should all be in the same <form> element:
<form method="POST">
The word description is: <?php echo $_POST['word_description']; ?>
<input type="hidden" name="word_description" value="<?php echo $_POST['word_description']?>">
<input type="text" name="word_name">
<input type="submit" name="submit_word" value="Провери дума.">
</form>
When the form is submitted, the $_POST array should contain the word_description AND the word_name submitted.
EDIT 2:
If you wish to use the id, you would have to first add it to your SELECT query:
$q = mysqli_query($connection, ' SELECT word_id, word_description
FROM words ORDER BY rand() LIMIT 1
');
Then you'd need to set it to some variable, and then later in your HTML:
<form method="POST">
The word description is: <?php echo $_POST['word_description']; ?>
<input type="hidden" name="word_id" value="<?php echo $word_id?>">
<input type="text" name="word_name">
<input type="submit" name="submit_word" value="Провери дума.">
</form>
Your second SQL query should then look like:
$q2="SELECT * FROM words WHERE word_name='$word_name' and word_id='".$_POST['word_id']."'";
Note: it is a bad practice to change the $_POST array in your code.
This array is populated by the request sent by the client and things can get confusing if you change the values there.
It is better to create another variable and set it to the value from the $_POST (example: $word_description = $_POST['word_description'];).
This way, you can still use array_key_exists('word_description', $_POST) to verify if the client actually sent something.
can you help me with displaying output from search form ?
My search form is in sidebar and i would like to display result on main content div.
Navigation is done by switch
this is form in sidebar
<form name="searchform" method="get" action="index.php?page=search">
<input type="text" name="searchword" size="15" />
<input type="submit" name="search" value="Click" class="formbutton" />
</form>
this is switch that should include output to the main content:
switch($_GET['page']){
case "search":
include("includes/search.php");
break;
}
and this is included search.php which should display result:
<?php
include('connect.php');
if(isset($_GET['search'])){
$search_value = $_GET['searchword'];
$query = " SELECT * FROM Articles WHERE keywords LIKE '$search_value'";
$run = mysql_query($query);
while($row=mysql_fetch_array($run)){
$post_id = $row['id'];
$post_title = $row['title'];
echo "<p><a href='index.php?page=post&&post=$post_id'>$post_title</a></p></ br>";
}
}
?>
What about if you specify a different page entirely in the form action instead of index.php? What you did is tell the form to use the index.php and send the GET data to it. If that doesn't help then be more specific on what you want
why does my 'if if (isset($_POST['submit'])) not interact with mysql'
Please tell me, where do I go wrong, I've been over this a thousand times and checked the internet... It's a lot of code, but I think I have overseen something. I made the remarks for myself, so I would'nt forget something stupid as an ";".
Thanks in advance
http://appsolute.vacau.com/cms
<html>
<head>
<title>Milan CMS</title>
</head>
<body>
<?php
mysql_connect("mysql15.000webhost.com", "a96443****tjiran", "ev****89") or die(mysql_error()); //opening database
mysql_select_db("a9644326_app") or die(mysql_error());
$result = mysql_query("select * from milan") or die(mysql_error()); //get msql database into $result
$content = mysql_fetch_array ($result); // make $content represent current entry
?>
<form action="<?php echo $PHP_SELF;?>" method="post" name="trainingen">
<h3>Trainingen:</h3><p><textarea name="nametrainingen" rows="10" cols="60"><?php echo $content['value']; ?></textarea></p>
<input type="submit" value="Verzenden"><input type="reset" value="Veld leeg maken">
</form>
<?php
if (isset($_POST['submit']))
{ //test if submit button is clicked
mysql_query("DELETE FROM milan WHERE tag='trainingen'") or die(mysql_error()); //delete old entry
$input = $_POST['nametrainingen']; //set $input as current entry
mysql_query("INSERT INTO milan (tag, value) VALUES ('trainingen', '$input')") or die(mysql_error()); //set tag, value as trainingen and $input(current entry)
$result = mysql_query("select * from milan") or die(mysql_error()); //reset $result to new database
$content = mysql_fetch_array ($result); //make $content represent new entry
$myFile = "trainingen.json";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, json_encode($content));
fclose($fh);
}
?>
<p>
</body>
</html>
$_POST is a super-global array that contains an associative array of all the form elements that were submitted with the form. You don't have an input with name="submit", so your isset() fails.
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="trainingen">
not $PHP_SELF
There is no tag whose "name" attribute is "submit" in your code.
Replace:
<input type="submit" value="Verzenden">
with:
<input name="submit" type="submit" value="Verzenden">
Try
<input type="submit" name="submit" value="Verzenden">
But warning -your code is insecure. Search for SQL injections in google.
In your submit button <input type="submit" value="Verzenden">. You have missed name="submit".
It should be
<input type="submit" value="Verzenden" name="submit">
The submit button doesn't pass anything through to the form. To confirm that, you can try var_dump($_POST). A substitute for that call would be
if (count($_POST))
However, you can still get notices and failed mysql queries so you should instead do
if (isset($_POST['nametrainingen']))
Long story short, I want users to be able to call the value of the variable $city_name into a string that they will submit.
Here's my code;
<?php
if(!isset($post_text)) {
$city_name = "Dallas";
$post_text = $_POST['post_text'];
echo($post_text);
}
?>
<html>
<head></head>
<body>
<form action="" method="post">
<input name="post_text" type="text" value="enter text here" />
<input type="submit" value="submit" />
</form>
I was under the impression that calling $city_name in the form field post_text would return "Dallas", unfortunately that's not the case.
If anyone has any advice or information on what alterations I could add to this code in order to allow the user to call the value of $city_name, it would be greatly appreciated :)!!
Based on your comment:
I would like for the user to be able to input and submit a string such as "I am going to $city_name" and have "I am going to Dallas" echoed on submit.
This is the code:
$city_name = "Dallas";
$post_text = str_replace('$city_name', $city_name, $_POST['post_text']);
echo $post_text;
It's important to use single quotes around '$city_name' here as you are searching for the literal text $city_name and not the contents of the variable $city_name.