Jump to content

.josh

Staff Alumni
  • Posts

    14,780
  • Joined

  • Last visited

  • Days Won

    43

Everything posted by .josh

  1. This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=338312.0
  2. Okay, it looks like you are trying to make an ajax request to the same script as the ajax request. This creates a blackhole in the internets. Basically the idea is when you make an AJAX call, it makes a request to some script, and if you want to "test" what is received, you have to output it (which you did with your echos)..but this just gets returned to your success callback function in your data variable. You have to then actually display it somewhere, like updating the innerHTML of a div or something. So basically it should be working for you, but you aren't "testing" it correctly. Here is a more simplified version, using a single script URL, with some comments: <?php // check if something is posted if ($_POST) { // dump out what is posted. This is what is going to be output for the AJAX call echo "<pre>";print_r($_POST); echo "</pre>"; // exit the script so that it doesn't output the rest of the stuff in the script exit(); } ?> <html> <head> <script src="jquery.js"></script> <script type="text/javascript"> var a = {}; a['test'] = 1; a['test2'] = 2; $.ajax({ url: "test.php", data: a, type: 'post', // this is your callback function success: function(data) { // update the contents of a div with the results of the ajax call $('#someDiv').html(data); } }); </script> </head> <body> <!-- div to update with AJAX results --> <div id='someDiv'></div> </body> </html>
  3. Please use [/ic] or [ic] tags when posting code. Also, please make a better attempt to explain your problem, starting with using proper English. I cannot understand what you are asking.
  4. $expr = '~(?:http://){0,1}(?:www.){0,1}youtube.com/watch\?.*?v=([^&[]++)[^[]*+~is';
  5. Run the content through htmlspecialchars or htmlentities
  6. Can you give more details? At face value, I think you're asking...if you have an update statement like update table set a=b where c=d You want to know which rows were matched in the c=d, but not actually updated? Well no...there is no way to find that out, because that's not how it works. All rows that match c=d will have a=b applied to it. IOW, no rows matched, therefore nothing was updated.
  7. yes, an empty action attribute means it posts to the same page. Hidden field method would just be something like <input type='hidden' name='contacts_form' value='true' /> Put that in your form and then server-side, look for that posted variable, same as any other posted variable. But base a condition around it. Dunno what language you are using but php example: if ($_POST['contacts_form']) { // do database insertion stuff here }
  8. $sentences = preg_split('~(.{1,998}\. )~s',$content,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  9. This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=338293.0
  10. document.getElementById looks for and returns an object reference of an html element with the specified id attribute (in this case, 'contact-form'). I'm assuming that you have a form on your page and the opening form tag looks something like <form name='something' id='contacts-form' method='post' action='somepage.php'> So the document.getElementById('contacts-form') is supposed to be returning a reference to your contact form, and then the .submit() is supposed to submit it, and all this is supposed to happen when you click that link, since it is inside the onclick attribute. So when you click on the link, that javascript code is executed and a request is made to whatever URL is in the action='...' attribute of your form tag. So you "detect" that the form has been submitted by virtue of the script running (whatever script is in the action attribute). If the URL in the action attribute is the same as the page the form is on, or shares the same URL with other pages, then you will have to append a variable to the URL or pass a hidden field or similar, to let the script know that it was requested because of your form submission and not something else.
  11. Just add them into the character class with the rest of the stuff. Only real trick is the hyphen, since it is also used to specify ranges. You can put it anywhere in the list if you escape it first (preceding it with a \) but you can also put it as the first or last item. Also, since you want to only allow those chars for the whole string, you need to include start and ending string anchor tags. Otherwise, it will match or not match based on substrings withing the string. var regex = /^[-a-zA-Z0-9_ ]+$/;
  12. requires jQuery...anyways, I'm not sure if you meant that you want only to dynamically append selected form elements, or if there are X amount you want to append for every row...I went with the former because that's harder. If it's the latter, this code can be simplified. <form id='someForm' action='' method='post'> <!-- row options to (multi)select --> <select id='rowOptions' name='rowOptions' multiple="multiple" size="3"> <option value="textField">text field</option> <option value="radioField">radio button</option> <option value="textAreaField">text area</option> </select> <br> <!-- button to dynamically append new form fields when clicked --> <input type='button' value='add row' id='addRow' /> <input type='submit' value='submit' id='submit' /> </form> <script type='text/javascript' src='jquery.js'></script> <script type='text/javascript'> // max number of rows to append var maxRows = 20; // current row to append var rowNum = 0; $(document).ready(function() { // on click of the 'add row' button... $('#addRow').click(function() { // if the current row is less than max rows ... if (rowNum < maxRows) { // variable to hold stuff to append var divRowContent = '<div id="rowDiv'+rowNum+'" style="border:solid green 1px;">'; // get the selected options var selected = $('#rowOptions').val(); // for each option selected... for (var x=0; x < selected.length; x++) { // add the form element to variable switch (selected[x]) { case 'textField' : divRowContent += '<input type="text" name="textField['+rowNum+']" id="textField'+rowNum+'" /><br>'; break; case 'radioField' : divRowContent += '<input type="radio" name="radioField['+rowNum+']" id="radioField'+rowNum+'_a" value="a" />a'; divRowContent += '<input type="radio" name="radioField['+rowNum+']" id="radioField'+rowNum+'_b" value="b" />b<br>'; break; case 'textAreaField' : divRowContent += '<textarea name="textAreaField['+rowNum+']" id="textAreaField'+rowNum+'" rows="2" cols="20"></textarea><br>'; break; } // end switch } // end for // closing div tag for the appending row divRowContent += '</div>'; // add row $('#someForm').append(divRowContent); // increment row counter rowNum++; } // end if }); // end .click }); // end .ready </script>
  13. Well my regex will work if you put some other attribute in there. It matches the rest of what's in the img tag by just looking for anything that is not the closing > for the tag.
  14. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=338149.0
  15. preg_match_all('~<img[^>]*(?<!_mce_)src\s?=\s?([\'"])((??!\1).)*)[^>]*>~i', $textareainfo, $output);
  16. Lots of times I see threads where people like to nitpick which method of doing something is faster, and it usually boils down to benchmarking the two (or more) methods...that is, executing each method lots of times and getting the average and seeing which one on average is faster. Here is a function I wrote that makes it easy to benchmark methods. Here is the function: <?php /** benchmark takes a list of user defined callback functions and calls each of them $rounds amount of times, recording the start and end times. It then calculates the average and then time it took to execute each function and then outputs some information. Basic principle is to find out which method of something is faster. @param integer $rounds The number of times the user defined function will be executed. This is not an optional parameter, but it defaults to 100. @param string argN The name of the user defined callback function to call. Every argument after the first argument will be used as a user defined function to call. */ function benchmark ($rounds=100) { // if an integer is not passed as first argument, default to 100 if (!is_integer($rounds)) $rounds = 100; // get the rest of the arguments passed to the function $funcs = func_get_args(); // remove first argument from the list, since it is $rounds array_shift($funcs); // for each user defined function... foreach ($funcs as $func) { // if the function doesn't exist, skip it if (!function_exists($func)) break; $time = array(); // call the function the specified/default amount of times for ($c = 0; $c < $rounds; $c++) { // get the current microtime timestamp $start = explode(" ",microtime()); $start = $start[0]; // call the user defined function $func(); // get the current microtime timestamp $end = explode(" ",microtime()); $end = $end[0]; // find out the difference between the two $diff = bcsub($end,$start,20); // for some unknown reason, $diff occasionally returns a negative number // dunno if it's a bug in bcsub or a bug in microtime or maybe sometimes // it just goes so damn fast it goes backwards in time! anyways...let's // just weed it out, make sure it doesn't skew the results. if ($diff > 0) $time[$c] = $diff; } // end $c // get the average time $average[$func] = rtrim(bcdiv(array_sum($time),count($time),20),'0'); } // end foreach funcs // sort the averages ascending (preserving the keys, because we used the // user defined function name as the array keys asort($average,SORT_NUMERIC); // get the fastest average $fastest = max($average); // get the slowest average $slowest = min($average); // display how many times we executed each function echo "times executed (each): ".$rounds . "<br/>"; // display the averages echo "fastest to slowest:<br/><pre>";print_r($average); echo "</pre>"; // display the time difference between slowest and fastest echo "biggest difference time: " . rtrim(bcsub($fastest,$slowest,20),'0') . "<br/>"; // calculate and display how much faster the fastest one was, compared // to the slowest, as a percentage $percent = rtrim(round(bcmul(bcsub(bcdiv($fastest,$slowest,20),1,20),100,20),4),'0'); echo "fastest is " . $percent . "% faster than the slowest"; } // end benchmark ?> Here is an example of how to use it. In this example, I want to compare 3 different regex patterns and see which one is on average the fastest. So I wrote a wrapper function for each one and then call the benchmark function: /* example: */ $content = "[tag]lots of stuff here[/tag]"; function lazy_matchall () { global $content; preg_match("~\[tag[^\]]*\](.*?)\[/tag\]~is", $content); } function greedy_matchall () { global $content; preg_match("~\[tag[^\]]*\](.*)\[/tag\]~is", $content); } function negative_lookahead () { global $content; preg_match("~\[tag[^\]]*\]((??!\[/tag\]).)*)~is", $content); } benchmark(10000,'lazy_matchall','greedy_matchall','negative_lookahead'); Output: times executed (each): 10000 fastest to slowest: Array ( [greedy_matchall] => 0.0000094208 [lazy_matchall] => 0.0000102794 [negative_lookahead] => 0.0000116116 ) biggest difference time: 0.0000021908 fastest is 23.2549% faster than the slowest
  17. you may also need to add the 's' modifier... technically linebreaks shouldn't be within p tags but it's perfectly valid for them to be, and some people like doing it to make the source code more readable in their editor.
  18. oops sorry..I was mixing visibility and display. <head> <style type='text/css'> #GENDER { display : none; } </style> <script type="text/javascript"> function HideGender(){ var table = document.getElementById("GENDER"); table.style.display = 'none'; } function DisplayGender(){ var row = document.getElementById("GENDER"); row.style.display = 'inline'; // or 'block' depending on the rest of your layout/preference } </script> </head>
  19. it hides it until you unhide it with your js onclick later. To which you need to use the correct value - "block" or "inline" not ""
  20. Use css to hide them. Put this in your head tag: <style type='text/css'> #GENDER { visibility : hidden; } </style>
  21. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=337713.0
  22. if you are just trying to get a true/false, you need to stick with preg_match. The problem is that unlike the posix (ereg) functions, the pcre (preg_xxx) functions require a delimiter to be wrapped around the pattern. It looks like you did do that, using "/". But since you chose that as your delimiter, you need to escape other instances of it in your pattern. Otherwise, when the engine gets to it in your pattern, it thinks that's the end of the pattern, and the stuff after the pattern delimiter are where modifiers go (like "i" for case-insensitive matches). So you have a / in there and it thinks the = after it is a modifier, and that's not a valid modifier. So long story short, escape instances of the delimiter you chose (you escape it by preceding it with a backslash: \/)
  23. onclick="return Minimum();"
  24. Okay so there's a couple different things wrong here. <select name='preset' onchange='preset(this);'> You name your select field and your function the same thing. In some browsers this won't work, because the name attribute will be made into a global property and overwrite your function. So you need to use a different function name, or else a different name attribute value for your select. <option value=test2'>testing 2</option> You have a typo, you are missing a quote. <input type='text' name='big[]' value='' /> I assume that overall you have more than one input field and are using big[] to to have an array of values, server-side. It's okay as-is, as far as your server-side code is concerned, because php automatically takes it and indexes it. But as far as client-side is concerned, when trying to reference it later, that's not going to work out. As far as client-side is concerned, you are naming lots of elements the same thing. So this needs to be changed to explicitly define the element index. So instead of 'big[]' you must do 'big[0]' (and also explicitly define other ones you have, like 'big[0]', 'big[1]', 'big[2]', etc...) if(ele=="test1") { so when you call preset(this) in your onchange, this is an object reference to the element, so in your function, ele is an object. if you want to check and see if the selected value is "test1", you need to use ele.value document.form."big[0]".value = action1; This is not the right way to reference the form input value. Normally it would be without quotes, as in document.form.big[0].value except that that won't work in this case, because of the [0]. So if it were just "big" you would do document.form.big.value. But since you are making it an array, you have to reference it like document.form["big[0]"].value Overall, here is what your code should look like: <form action='submit.php' method='POST' name='form'> <select name='preset' onchange='presetX(this);'> <option value='test1'>testing 1</option> <option value='test2'>testing 2</option> </select> <input type='text' name='big[0]' value='' /> </form> <script type='text/javascript'> function presetX(ele) { if(ele.value=="test1") { var action1 = "testing 3"; } else { var action1 = "testing 4"; } document.form["big[0]"].value = action1; } </script>
  25. Basically it sounds like people want like what SO does, except nobody has mentioned downvoting replies
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.