Get text area value as php variable - javascript

I have a text area field and I want to get value as variable so that I can use these variable to fetch data from mysql.
<?
$sms = $_POST['message'];
$whatIWant = substr($sms, strpos($sms, "#") + 1);
echo $whatIWant;
?>
this is a image of textarea
I tried these code but it's not output as I want.
How can I retrieve variables from this input value? Thanks

You can try something like:
$str = 'Hello #Fahim#. Im from #Mazegeek,Inc#. My phone no is #011111#';
preg_match_all('/(?<=#).*?(?=#)/', $str, $m);
print_r($m);
let me know if it works.

Related

Is there anyway to store dynamic PHP value in JavaScript variable?

I have a JavaScript function for scrolling of table when button is click but I have multiple tables in my code and cant use multiple function for each table I want to store the id's of all table in dynamic way using PHP.
<script type="text/javascript">
// Do something in JavaScript
var x = <?php echo $row_[document_id]; ?>;
// etc..
</script>
document_id is were value are accessed dynamically in PHP.
Instead of using
var x = <?php echo $row_[document_id]; ?>;
you can use something like this:
<?php
echo '<script>var x = ' . $row_document_id . ';</script>';
?>
If data is a string, you can use double quotes:
<?php
echo '<script>var x = "' . $row_document_id . '";</script>';
?>
Also it is preferred to use let instead of var.
Hope that helped.
Save the PHP variable value in HTML hidden input element.
<input type="hidden" value="<?php echo $row_[document_id]?>" id="getValue">
And then get the value from JavaScript or jQuery.
var x = $("#getValue").val();

How to get dynamically changing value from external url in PHP?

I'm trying to get a quantity value from external link, but I can't see this value until particular colour or size is selected (selection on that website works using JavaScript void(0) ).
Is it possible to trigger a link somehow and get the value after? Any suggestions?
However I know how to get a static value from url, see below:
$url = 'http://www.website.com/page.html';
$content = file_get_contents($url);
$first_step = explode( '<span id="quantity">' , $content );
$second_step = explode("</span>" , $first_step[1] );
echo $second_step[0];
Maybe solution, you can to split the process into two parts :
get all elements with regexp
preg_match_all('/<span [^>]+>/i',$content , $match);
print_r($match);
search attrs array of the result
$spans = array();
foreach( $match as $tag)
{
preg_match_all('/(id)=("[^"]*")/i',$tag, $spans[$tag]);
}
print_r($spans);

Pass a variable from JavaScript to PHP

I'm trying to pass the variable from a JavaScript function - selected text - to the same page using php post method:
if (isset($_POST['u_name']))
{
echo $_POST['u_name'] . '</p>';
}
echo "<script type='text/javascript'>";
echo "var var1 = 0; var range = window.getSelection ();";
echo "function gst () { var range = window.getSelection (); alert (range.toString ()); var1 = range.toString ();}";
echo "document.write('<form method=\'post\'>');";
echo "document.write('<p>selected area:<br />');";
echo "document.write('<button onclick=\'gst ()\' type=\'submit\' name=\'u_name\' value = \'' + var1 + ' \' />Button</button>');";
echo "document.write('</form>');";
echo "alert (interesting);";
echo "</script>";
after pressing the button the selected page text is correct: it is checked with alert (range.toString ()) , however, the initial value of var1 variable - 0 is posted.
What could cause it and how one can pass the value, obtained from the javascript function through post method ?
Anton
That's because you set value attribute on the page load.
You can change it dynamically on button click. Replace one of your rows to:
echo "document.write('<button onclick=\"this.setAttribute(\'value\', var1); gst()\" type=\'submit\' name=\'u_name\' value = \'' + var1 + ' \' />Button</button>');";
Note this.setAttribute.
If you want to pass JavaScript variables to PHP, you'll have to use an Ajax request.
PHP is server sided, whereas JavaScript is client sided. This means that all PHP code is done before any JavaScript is even triggered. You can manipulate JavaScript with PHP, but if you want to manipulate PHP with JavaScript, use an Ajax call.

String replacement of mixed text and HTML (client or server-side)

I want to replace strings of text and html on a webpage. This is for the purpose of highlighting pieces of text. So, highlighted text is stored in a database, and when its webpage is loaded, the code should find and replace the piece of text.
For example,
$original_string = "SOME TEXT";
$replacement_string = "<span data-id='7334' class='highlight'>SOME TEXT</span>";
I had issues using PHP str_replace, as it wasn't very good with replacing weird html entities or larger strings.
So now, I am trying to do it on the client-side using JS, and have had much better results using js replace().
My question is - what is the best way to achieve the above goal? Should I use a PHP parser or stick with JS ?
And if I use JS, how should I structure the for loop so that all replacements are made on the same text? This did not quite work:
var value = $('#domainContainer').html();
var $highlightsArray_search = <?php echo json_encode($highlightsArray_search); ?>;
var $highlightsArray_replace = <?php echo json_encode($highlightsArray_replace); ?>;
for(var i=0; i<2; i++){
var search_value = $highlightsArray_search[i];
var replace_value = $highlightsArray_replace[i];
var formattedSnippet = value.replace(search_value, replace_value);
value = formattedSnippet;
}
$('#domainContainer').html(formattedSnippet);
You have to wrap php code with quotes .Just change like this,
var $highlightsArray_search = "<?php echo json_encode($highlightsArray_search); ?>;"
^ ^
var $highlightsArray_replace = "<?php echo json_encode($highlightsArray_replace); ?>;"
^ ^
Take your html code in to php string for example.
<?php
$original_string = "SOME TEXT";
$first ="<span data-id='7334' class='highlight'>";
$second = "</span>";
echo $first.$original_string.$second;
?>
Try this may be helpful

Error when sending html source from javascript to PHP

I am using the following to encode the html source of a ckeditor in a web application.
var updateString = app.getValue('wysiwygHomePage');
var encodedString = encodeURIComponent(updateString);
alert(encodedString);
app.httpRequest("www.xxxx.com/techy/savealldata.php", "GET", function(data, error, httpResponse){
alert(data);
},
{
"updateType":"homePage","updateString":encodedString}, "String", {}, {});
}
Then at the PHP end I am using :
<?php
$updateType = $_GET["updateType"];
$updateString = $_GET["updateString"];
$updateString2 = urldecode($updateString);
echo 'success here '.$updateType .' '.$updateString2 ;
?>
I am adding some coloured tex and the html source for this is:
<p>
<span style="color: rgb(255, 140, 0);">123</span><br />
</p>
<p>
This works okay until I cut and paste more than 32 times.
I then just get error returned from the PHP call.
I presume there are to many chars arriving at the PHP end ???
Any ideas why this is happening ?
Mr WARBY.
UPDATED PHP Code.
<?php
include 'dbdata.php';
$updateType = $_POST["updateType"];
$updateString = $_POST["updateString"];
$updateString2 = urldecode($updateString);
//echo 'success here '.$updateType .' '.$updateString2 ;
if($updateType === 'homePage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 12";
//echo $query5;
echo 'Home Page Updated 2';
mysql_query($query5);
}
if($updateType === 'instructionPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 13";
echo 'Instruction Page Updated 2';
mysql_query($query5);
}
if($updateType === 'FAQPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 14";
echo 'FAQ Page Updated';
mysql_query($query5);
}
?>
There are a lot of variables in play here. You need to change your debugging strategy. Instead of testing end to end each time try isolating each component.
In Javascript, call "app.getValue('wysiwygHomePage')", encode the string, decode the string, and put it right back in the editor. Do that in a loop until you can determine if the client-side is mangling anything.
If not, try encoding a complicated string in Javascript, sending it to a PHP script that decodes/re-encodes and echos it back. Do that in a loop several times.
If you still haven't found the problem try making a PHP script that takes a complicated string, INSERTS it, SELECTs it, UPDATEs it in a loop to see if you database encoding or escaping is affecting it.
If at any point you find the string changing when it shouldn't you've probably found your problem.

Categories